diff options
Diffstat (limited to '2021/09/code.tl')
-rw-r--r-- | 2021/09/code.tl | 59 |
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)) |