You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
898 B
Racket
30 lines
898 B
Racket
#lang reader "../aoc-lang.rkt"
|
|
(require graph)
|
|
(provide (rename-out [#%mb #%module-begin]))
|
|
|
|
(define-macro (#%mb (STARS) (NUM <-> . NUMS) ...)
|
|
#'(#%module-begin
|
|
(time
|
|
(define g (unweighted-graph/undirected null))
|
|
(for-each (curry add-edge! g NUM) (list . NUMS)) ...
|
|
(if (eq? 'STARS '★)
|
|
(programs-in-group g 0)
|
|
(number-of-groups g (list NUM ...))))))
|
|
|
|
(define (programs-in-group g x) (length (group-of g x)))
|
|
|
|
(define (group-of g x)
|
|
(define-values (connects _) (dijkstra g x))
|
|
(for/list ([(k v) (in-mutable-hash connects)]
|
|
#:when (integer? v))
|
|
k))
|
|
|
|
(define (number-of-groups g nums)
|
|
(for/fold ([nums-seen null]
|
|
[group-count 0]
|
|
#:result group-count)
|
|
([num (in-list nums)]
|
|
#:unless (memv num nums-seen))
|
|
(values (append (group-of g num) nums-seen) (add1 group-count))))
|
|
|