This commit is contained in:
Jonathan Chan 2021-12-11 22:56:51 -08:00
parent 6912f22c1a
commit 6f797ccca2
2 changed files with 62 additions and 0 deletions

21
input/12.txt Normal file
View File

@ -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

41
src/12.rkt Normal file
View File

@ -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))