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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
aoc-racket/2017/d12/main.rkt

29 lines
873 B
Racket

#lang reader "../aoc-lang.rkt"
(require graph)
(provide (rename-out [#%mb #%module-begin]))
(define-macro (#%mb (STARS) (NUM <-> . NUMS) ...)
#'(#%module-begin
(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-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))))