summaryrefslogtreecommitdiffstats
path: root/2021/12/one.tl
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-11-06 09:58:38 -0800
committerKaz Kylheku <kaz@kylheku.com>2022-11-06 09:58:38 -0800
commit4fd1aae518076adc8b97735225c678d6a362328d (patch)
tree97d61b659fc3cac628d0cdee71128a0baee2cb73 /2021/12/one.tl
downloadadvent-4fd1aae518076adc8b97735225c678d6a362328d.tar.gz
advent-4fd1aae518076adc8b97735225c678d6a362328d.tar.bz2
advent-4fd1aae518076adc8b97735225c678d6a362328d.zip
Kazinator's Advent of Code stuff.
Diffstat (limited to '2021/12/one.tl')
-rw-r--r--2021/12/one.tl49
1 files changed, 49 insertions, 0 deletions
diff --git a/2021/12/one.tl b/2021/12/one.tl
new file mode 100644
index 0000000..70e36d5
--- /dev/null
+++ b/2021/12/one.tl
@@ -0,0 +1,49 @@
+(defvar %cave-hash% (hash))
+
+(defstruct cave ()
+ name
+ neighbors
+
+ (:postinit (cv)
+ (assert cv.name)
+ (set [%cave-hash% cv.name] cv)))
+
+(defstruct small-cave cave
+ (:method visit (cv path fn)
+ (unless (member cv path)
+ (push cv path)
+ (each ((n cv.neighbors))
+ n.(visit path fn)))))
+
+(defstruct big-cave cave
+ (:method visit (cv path fn)
+ (push cv path)
+ (each ((n cv.neighbors))
+ n.(visit path fn))))
+
+(defstruct end-cave cave
+ (:method visit (cv path fn)
+ (push cv path)
+ [fn path]))
+
+(defun ensure-cave (name)
+ (or [%cave-hash% name]
+ (set [%cave-hash% name]
+ (match-case name
+ ("end" (new end-cave name name))
+ (@[all @str chr-islower] (new small-cave name name))
+ (@else (new big-cave name name))))))
+
+(defun read-caves (: (name "input"))
+ (with-stream (s (open-file name))
+ (whilet ((line (get-line s)))
+ (match `@a-@b` line
+ (let ((ca (ensure-cave a))
+ (cb (ensure-cave b)))
+ (pushnew cb ca.neighbors)
+ (pushnew ca cb.neighbors))))))
+
+(defun find-paths (fn)
+ (let ((start [%cave-hash% "start"]))
+ (assert start)
+ start.(visit nil fn)))