You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.2 KiB
52 lines
1.2 KiB
#lang racket |
|
(require "lib/common.rkt" |
|
fancy-app |
|
graph) |
|
|
|
(define (big-cave? vtx) |
|
(for/and ([ch (in-string vtx)]) |
|
(char-upper-case? ch))) |
|
|
|
(define (parse lines) |
|
(undirected-graph (map (string-split _ "-") lines))) |
|
|
|
(define (walk G did-twice? [vertex "start"] [seen (set "start")]) |
|
(for*/sum ([adj (in-neighbors G vertex)] |
|
[big? (in-value (big-cave? adj))] |
|
[seen? (in-value (set-member? seen adj))] |
|
#:when (or big? (not seen?) (not did-twice?)) |
|
#:unless (equal? adj "start")) |
|
(cond [(string=? adj "end") 1] |
|
[else (walk G |
|
(or did-twice? (and (not big?) seen?)) |
|
adj |
|
(set-add seen adj))]))) |
|
|
|
(define (day12a G) |
|
(walk G #t)) |
|
|
|
(define (day12b G) |
|
(walk G #f)) |
|
|
|
(module+ main |
|
(call-with-input-file "data/day12.txt" |
|
(λ (prt) |
|
(define lines (parse (port->lines prt))) |
|
(answer 12 1 (day12a lines)) |
|
(answer 12 2 (day12b lines))))) |
|
|
|
(module+ test |
|
(require rackunit) |
|
|
|
(define lines #<<EOD |
|
start-A |
|
start-b |
|
A-c |
|
A-b |
|
b-d |
|
A-end |
|
b-end |
|
EOD |
|
) |
|
(check-equal? (day12a (parse (string-split lines))) 10) |
|
(check-equal? (day12b (parse (string-split lines))) 36))
|
|
|