Day 12.
This commit is contained in:
parent
6912f22c1a
commit
6f797ccca2
|
@ -0,0 +1,21 @@
|
|||
ma-start
|
||||
YZ-rv
|
||||
MP-rv
|
||||
vc-MP
|
||||
QD-kj
|
||||
rv-kj
|
||||
ma-rv
|
||||
YZ-zd
|
||||
UB-rv
|
||||
MP-xe
|
||||
start-MP
|
||||
zd-end
|
||||
ma-UB
|
||||
ma-MP
|
||||
UB-xe
|
||||
end-UB
|
||||
ju-MP
|
||||
ma-xe
|
||||
zd-UB
|
||||
start-xe
|
||||
YZ-end
|
|
@ -0,0 +1,41 @@
|
|||
#lang curly-fn racket
|
||||
|
||||
(require racket/set
|
||||
"../lib.rkt")
|
||||
|
||||
(define input
|
||||
(for/fold ([caves (hash)])
|
||||
([connection (problem-input 12)])
|
||||
(match-let* ([(list cave1 cave2) (string-split connection "-")]
|
||||
[caves (hash-update caves cave1 #{set-add % cave2} (set))]
|
||||
[caves (hash-update caves cave2 #{set-add % cave1} (set))])
|
||||
caves)))
|
||||
|
||||
(struct path+ (path small-caves twice?))
|
||||
|
||||
(define (small? cave)
|
||||
(char-lower-case? (string-ref cave 0)))
|
||||
|
||||
(define (get-paths once?)
|
||||
(let loop ([queue (list (path+ '("start") (set) once?))]
|
||||
[paths 0])
|
||||
(match queue
|
||||
['() paths]
|
||||
[(list (path+ path small-caves twice?) queue ...)
|
||||
(define adjacent
|
||||
(hash-ref input (first path)))
|
||||
(define visitable
|
||||
(if twice?
|
||||
(set-subtract adjacent (set "start" "end") small-caves)
|
||||
(set-subtract adjacent (set "start" "end"))))
|
||||
(define queue*
|
||||
(for/list ([cave visitable])
|
||||
(path+ (cons cave path)
|
||||
(if (small? cave) (set-add small-caves cave) small-caves)
|
||||
(or (set-member? small-caves cave) twice?))))
|
||||
(define paths*
|
||||
(if (set-member? adjacent "end")
|
||||
(add1 paths) paths))
|
||||
(loop (append queue* queue) paths*)])))
|
||||
|
||||
(show-solution (get-paths #t) (get-paths #f))
|
Loading…
Reference in New Issue