Add Advent 2021 - day 12.

This commit is contained in:
Érico Nogueira 2021-12-12 04:26:45 -03:00
parent 31e1fa2682
commit 30b0110035
4 changed files with 90 additions and 0 deletions

43
2021/day12/p.rkt Normal file
View File

@ -0,0 +1,43 @@
#lang racket/base
(require racket/string)
(require racket/set)
(define (cdrm l)
(car (cdr l)))
(define (make-line t)
(sort (string-split t "-") string<?))
(define sset (for/set ([t (in-lines)]) (make-line t)))
(define caves (for/mutable-set ([s sset]) (car s)))
(set-union! caves (for/set ([s sset]) (cdrm s)))
(set-subtract! caves (set "start" "end"))
(define (is-small s)
(char-lower-case? (string-ref s 0)))
(define (valid-path p1 p2)
(set-member? sset (sort (list p1 p2) string<?)))
(define (cannot-use-cave c used)
;(hash-has-key? used c)) ; part 1
(and (hash-has-key? used c) (hash-has-key? used 0))) ; part 2
(define (hash-handle! used c)
(hash-update! used c add1 0)
(when (= (hash-ref used c) 2)
(hash-set! used 0 #t)))
(for/sum ([cave caves])
(define (recurse-path path used c)
(when (is-small c) (hash-handle! used c))
(set! path (append path (list c)))
(+ (if (valid-path "end" c) 1 0)
(for/sum ([nc caves]
#:unless (or (equal? nc c) (cannot-use-cave nc used) (not (valid-path c nc))))
(recurse-path path (hash-copy used) nc))))
(if (valid-path "start" cave)
(recurse-path null (make-hash) cave)
0))

7
2021/day12/test1.txt Normal file
View File

@ -0,0 +1,7 @@
start-A
start-b
A-c
A-b
b-d
A-end
b-end

18
2021/day12/test2.txt Normal file
View File

@ -0,0 +1,18 @@
fs-end
he-DX
fs-he
start-DX
pj-DX
end-zg
zg-sl
zg-pj
pj-he
RW-he
fs-DX
pj-RW
zg-RW
start-pj
he-WI
zg-he
pj-fs
start-RW

22
2021/day12/test3.txt Normal file
View File

@ -0,0 +1,22 @@
end-MY
MY-xc
ho-NF
start-ho
NF-xc
NF-yf
end-yf
xc-TP
MY-qo
yf-TP
dc-NF
dc-xc
start-dc
yf-MY
MY-ho
EM-uh
xc-yf
ho-dc
uh-NF
yf-ho
end-uh
start-NF