day 4 part 1
This commit is contained in:
parent
e48ca63881
commit
6c8340752c
2 changed files with 63 additions and 0 deletions
53
04a.lisp
Normal file
53
04a.lisp
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
(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"))
|
||||
10
04example.txt
Normal file
10
04example.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
..@@.@@@@.
|
||||
@@@.@.@.@@
|
||||
@@@@@.@.@@
|
||||
@.@@@@..@.
|
||||
@@.@@@@.@@
|
||||
.@@@@@@@.@
|
||||
.@.@.@.@@@
|
||||
@.@@@.@@@@
|
||||
.@@@@@@@@.
|
||||
@.@.@@@.@.
|
||||
Loading…
Add table
Add a link
Reference in a new issue