Initial commit
This commit is contained in:
commit
f242d2b0df
420 changed files with 62521 additions and 0 deletions
59
2021/day12/day12.scm
Normal file
59
2021/day12/day12.scm
Normal file
|
@ -0,0 +1,59 @@
|
|||
(import (chicken io)
|
||||
srfi-1
|
||||
srfi-42
|
||||
srfi-152)
|
||||
|
||||
(define (read-input #!optional (port (current-input-port)))
|
||||
(map (lambda (s) (string-split s "-")) (read-lines port)))
|
||||
|
||||
(define (big-cave? cave)
|
||||
(char-upper-case? (string-ref cave 0)))
|
||||
|
||||
(define (small-cave? cave)
|
||||
(char-lower-case? (string-ref cave 0)))
|
||||
|
||||
(define (neighbours edges vertex)
|
||||
(let lp ((acc '())
|
||||
(edges edges))
|
||||
(cond
|
||||
((null? edges) acc)
|
||||
((equal? vertex (car (car edges))) (lp (cons (cadr (car edges)) acc) (cdr edges)))
|
||||
((equal? vertex (cadr (car edges))) (lp (cons (car (car edges)) acc) (cdr edges)))
|
||||
(else (lp acc (cdr edges))))))
|
||||
|
||||
(define (find-all-paths edges start end)
|
||||
(define paths '())
|
||||
(let lp ((path (list start)))
|
||||
(if (equal? (car path) end)
|
||||
(set! paths (cons (reverse path) paths))
|
||||
(do-ec (: n (neighbours edges (car path)))
|
||||
(or (big-cave? n)
|
||||
(not (member n path)))
|
||||
(lp (cons n path)))))
|
||||
paths)
|
||||
|
||||
(define (part1 edges)
|
||||
(length (find-all-paths edges "start" "end")))
|
||||
|
||||
(define (small-caves-at-most-once-except-one? path)
|
||||
(let ((small-caves (filter small-cave? path)))
|
||||
(<= (- (length small-caves) (length (delete-duplicates small-caves))) 1)))
|
||||
|
||||
(define (find-all-paths-2 edges start end)
|
||||
(define paths '())
|
||||
(let lp ((path (list start)))
|
||||
(if (equal? (car path) end)
|
||||
(set! paths (cons (reverse path) paths))
|
||||
(do-ec (: n (neighbours edges (car path)))
|
||||
(and (not (equal? n "start"))
|
||||
(or (big-cave? n)
|
||||
(small-caves-at-most-once-except-one? path)))
|
||||
(lp (cons n path)))))
|
||||
paths)
|
||||
|
||||
(define (part2 edges)
|
||||
(length (find-all-paths-2 edges "start" "end")))
|
||||
|
||||
(let ((edges (read-input)))
|
||||
(print (part1 edges))
|
||||
(print (part2 edges)))
|
23
2021/day12/input
Normal file
23
2021/day12/input
Normal file
|
@ -0,0 +1,23 @@
|
|||
dr-of
|
||||
start-KT
|
||||
yj-sk
|
||||
start-gb
|
||||
of-start
|
||||
IJ-end
|
||||
VT-sk
|
||||
end-sk
|
||||
VT-km
|
||||
KT-end
|
||||
IJ-of
|
||||
dr-IJ
|
||||
yj-IJ
|
||||
KT-yj
|
||||
gb-VT
|
||||
dr-yj
|
||||
VT-of
|
||||
PZ-dr
|
||||
KT-of
|
||||
KT-gb
|
||||
of-gb
|
||||
dr-sk
|
||||
dr-VT
|
7
2021/day12/test1
Normal file
7
2021/day12/test1
Normal file
|
@ -0,0 +1,7 @@
|
|||
start-A
|
||||
start-b
|
||||
A-c
|
||||
A-b
|
||||
b-d
|
||||
A-end
|
||||
b-end
|
10
2021/day12/test2
Normal file
10
2021/day12/test2
Normal file
|
@ -0,0 +1,10 @@
|
|||
dc-end
|
||||
HN-start
|
||||
start-kj
|
||||
dc-start
|
||||
dc-HN
|
||||
LN-dc
|
||||
HN-end
|
||||
kj-sa
|
||||
kj-HN
|
||||
kj-dc
|
18
2021/day12/test3
Normal file
18
2021/day12/test3
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue