-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathday18.lisp
More file actions
56 lines (44 loc) · 2.01 KB
/
day18.lisp
File metadata and controls
56 lines (44 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
(defpackage :aoc/2018/18 #.cl-user::*aoc-use*)
(in-package :aoc/2018/18)
(defun parse-area (data)
(let ((rows (length data))
(cols (length (first data))))
(make-array (list rows cols) :initial-contents data)))
(defun part1 (area iterations)
(dotimes (i iterations)
(setf area (area-next area)))
(resource-value area))
(defun area-next (area)
(let* ((rows (array-dimension area 0))
(cols (array-dimension area 1))
(next (make-array (list rows cols))))
(loop :for r :below rows :do
(loop for c :below cols :do
(loop :with open = 0 :and trees = 0 :and lumby = 0
:for rr :from (max (1- r) 0) :upto (min (1+ r) (1- rows)) :do
(loop :for cc :from (max (1- c) 0) :upto (min (1+ c) (1- cols))
:unless (and (= rr r) (= cc c)) :do
(ecase (aref area rr cc)
(#\. (incf open))
(#\| (incf trees))
(#\# (incf lumby))))
:finally (setf (aref next r c)
(ecase (aref area r c)
(#\. (if (>= trees 3) #\| #\.))
(#\| (if (>= lumby 3) #\# #\|))
(#\# (if (and (>= lumby 1) (>= trees 1))
#\# #\.)))))))
next))
(defun resource-value (area)
(loop :for i :below (array-total-size area) :for ch = (row-major-aref area i)
:count (eq ch #\|) :into trees
:count (eq ch #\#) :into lumby
:finally (return (* trees lumby))))
(defun part2 (area)
(destructuring-bind (cycles-at cycle-size area-new)
(floyd #'area-next area :test 'equalp)
(let ((remaining (rem (- 1000000000 cycles-at) cycle-size)))
(part1 area-new remaining))))
(define-solution (2018 18) (area parse-area)
(values (part1 area 10) (part2 area)))
(define-test (2018 18) (549936 206304))