aoc-2025/04a.lisp
2025-12-10 20:19:45 +11:00

53 lines
1.7 KiB
Common Lisp

(require :uiop)
(load (sb-ext:posix-getenv "ASDF"))
(asdf:load-system 'str)
(defun read-diagram (lines)
(let* (
(dim (length lines))
(diagram (make-array (list dim dim))))
(loop for line in lines
for y from 0
do (loop for char across line
for x from 0
do (if (char-equal char #\@)
(setf (aref diagram y x) 1))))
diagram))
(read-diagram (uiop:read-file-lines "04example.txt"))
(defun make-kernel (diagram -x -y)
(let ( (kernel (make-array '(3 3))))
(loop for y from -1 to 1
do (loop for x from -1 to 1
do (let ((-x (+ -x x))
(-y (+ -y y)))
(if (array-in-bounds-p diagram -y -x)
(setf (aref kernel (+ y 1) (+ x 1)) (aref diagram -y -x))
(setf (aref kernel (+ y 1) (+ x 1)) 0)))))
kernel))
(make-kernel (read-diagram (uiop:read-file-lines "04example.txt")) 0 2)
(defun sum-kernel (kernel)
(if (= (aref kernel 1 1) 1)
(if
(<
(-
(loop for y from 0 to 2
sum (loop for x from 0 to 2
sum (aref kernel y x)))
1)
4)
1
0)
0))
(sum-kernel (make-kernel (read-diagram (uiop:read-file-lines "04example.txt")) 2 0))
(defun sum-diagram (lines)
(let ( (dim (length lines))
(diagram (read-diagram lines)))
(loop for y from 0 to dim
sum (loop for x from 0 to dim
sum (sum-kernel (make-kernel diagram x y))))))
(sum-diagram (uiop:read-file-lines "04example.txt"))
(sum-diagram (uiop:read-file-lines "04data.txt"))