summaryrefslogtreecommitdiffstats
path: root/2021/09/code.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/09/code.tl
downloadadvent-4fd1aae518076adc8b97735225c678d6a362328d.tar.gz
advent-4fd1aae518076adc8b97735225c678d6a362328d.tar.bz2
advent-4fd1aae518076adc8b97735225c678d6a362328d.zip
Kazinator's Advent of Code stuff.
Diffstat (limited to '2021/09/code.tl')
-rw-r--r--2021/09/code.tl59
1 files changed, 59 insertions, 0 deletions
diff --git a/2021/09/code.tl b/2021/09/code.tl
new file mode 100644
index 0000000..a0e223e
--- /dev/null
+++ b/2021/09/code.tl
@@ -0,0 +1,59 @@
+(defstruct board ()
+ w h a
+
+ (:method lambda (me x y)
+ (if (or (minusp x)
+ (minusp y)
+ (<= me.w x)
+ (<= me.h y))
+ #\9
+ [[me.a y] x]))
+
+ (:method lambda-set (me x y nv)
+ (set [[me.a y] x] nv)))
+
+(defun read-input (: (name "input"))
+ (flow (file-get-lines name)
+ vec-list
+ (new board
+ h (len @1)
+ w (len (first @1))
+ a @1)))
+
+;;-----------------------------------------------------------------------------
+
+(defun solve-one (bo)
+ (sum-each-prod ((y 0..bo.h)
+ (x 0..bo.w))
+ (let ((c [bo x y])
+ (u [bo x (pred y)])
+ (d [bo x (succ y)])
+ (l [bo (pred x) y])
+ (r [bo (succ x) y]))
+ (if (and (< c u) (< c d) (< c l) (< c r))
+ (+ 1 (chr-digit c))
+ 0))))
+
+;;-----------------------------------------------------------------------------
+
+(defmeth board basin-size (bo x y)
+ (caseq [bo x y]
+ ((#\9 #\space) 0)
+ (t (set [bo x y] #\space)
+ (+ 1
+ bo.(basin-size x (pred y))
+ bo.(basin-size x (succ y))
+ bo.(basin-size (pred x) y)
+ bo.(basin-size (succ x) y)))))
+
+(defun solve-two (bo)
+ (flow
+ (build
+ (each-prod ((y 0..bo.h)
+ (x 0..bo.w))
+ (let* ((bs bo.(basin-size x y)))
+ (if (plusp bs)
+ (add bs)))))
+ sort
+ (nthlast 3)
+ prod))