This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem-11.lisp
executable file
·209 lines (191 loc) · 6.31 KB
/
problem-11.lisp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
;;; Problem: What is the greatest product of four adjacent numbers in any
;;; direction (up, down, left, right, or diagonally) in the 20×20
;;; grid in problem-11.txt?
;;; (time (largest-product))
(defun string-to-list (s)
(assert (stringp s) (s) "~s should be a string but is not")
(coerce s 'list))
(defun read-lines ()
(let ((in (open "problem-11.txt" :if-does-not-exist nil))
(lines nil))
(when in
(loop for line = (read-line in nil)
while line do
(setf lines (append lines (cons line nil))))
(close in))
lines))
(defun tokenize (string)
(let ((list (string-to-list string))
(buffer nil)
(ret nil))
(loop for c in list do
(if (or (equal c '#\Space) (equal c '#\Newline))
(progn
(setf ret (append ret (cons
(concatenate 'string buffer)
nil)))
(setf buffer nil))
(setf buffer (append buffer (cons c nil)))))
(setf ret (append ret (cons
(concatenate 'string buffer)
nil)))
ret))
(defun consolidate-list (list)
(let ((vec (eval `(vector ,@list))))
(loop for i from 0 to (- (length vec) 1) do
(setf (svref vec i) (parse-integer (svref vec i))))
vec))
(defun create-array ()
(let ((list nil)
(lines (read-lines))
(height 0)
(width 0)
(line ""))
(loop for l in lines do
(setf line (consolidate-list (tokenize l)))
(setf width (length line))
(setf list (append list (cons line nil))))
(setf height (length list))
(let ((array (make-array `(,height ,width))))
(loop for h from 0 to (- height 1) do
(loop for w from 0 to (- width 1) do
(setf
(aref array h w)
(svref (nth h list) w))))
array)))
(defun product (vector)
(let ((prod 1)
(vsize (- (length vector) 1)))
(loop for i from 0 to vsize do
(setf prod (* prod (svref vector i))))
prod))
(defun largest-horizontal-forward (array)
(let ((largest 0)
(buffer (make-array 4))
(height (array-dimension array 0))
(width (array-dimension array 1)))
(loop for y from 0 to (- height 1) do
(loop for x from 0 to (- width 4) do
(loop for i from 0 to 3 do
(setf (svref buffer i)
(aref array y (+ x i))))
(if (> (product buffer) largest)
(progn
(setf largest (product buffer))))))
largest))
(defun largest-horizontal-back (array)
(let ((largest 0)
(buffer (make-array 4))
(height (array-dimension array 0))
(width (array-dimension array 1)))
(loop for y from 0 to (- height 1) do
(loop for x from 3 to (- width 1) do
(loop for i from 0 to 3 do
(setf (svref buffer i)
(aref array y (- x i))))
(if (> (product buffer) largest)
(progn
(setf largest (product buffer))))))
largest))
(defun largest-vertical-down (array)
(let ((largest 0)
(buffer (make-array 4))
(height (array-dimension array 0))
(width (array-dimension array 1)))
(loop for x from 0 to (- width 1) do
(loop for y from 0 to (- height 4) do
(loop for i from 0 to 3 do
(setf (svref buffer i)
(aref array (+ y i) x)))
(if (> (product buffer) largest)
(progn
(setf largest (product buffer))))))
largest))
(defun largest-vertical-up (array)
(let ((largest 0)
(buffer (make-array 4))
(height (array-dimension array 0))
(width (array-dimension array 1)))
(loop for x from 0 to (- width 1) do
(loop for y from 3 to (- height 1) do
(loop for i from 0 to 3 do
(setf (svref buffer i)
(aref array (- y i) x)))
(if (> (product buffer) largest)
(progn
(setf largest (product buffer))))))
largest))
(defun largest-diagonal-right-down (array)
(let ((largest 0)
(buffer (make-array 4))
(height (array-dimension array 0))
(width (array-dimension array 1)))
(loop for y from 0 to (- height 4) do
(loop for x from 0 to (- width 4) do
(loop for i from 0 to 3 do
(setf (svref buffer i)
(aref array (+ y i) (+ x i))))
(if (> (product buffer) largest)
(progn
(setf largest (product buffer))))))
largest))
(defun largest-diagonal-left-down (array)
(let ((largest 0)
(buffer (make-array 4))
(height (array-dimension array 0))
(width (array-dimension array 1)))
(loop for y from 0 to (- height 4) do
(loop for x from 3 to (- width 1) do
(loop for i from 0 to 3 do
(setf (svref buffer i)
(aref array (+ y i) (- x i))))
(if (> (product buffer) largest)
(progn
(setf largest (product buffer))))))
largest))
(defun largest-diagonal-right-up (array)
(let ((largest 0)
(buffer (make-array 4))
(height (array-dimension array 0))
(width (array-dimension array 1)))
(loop for y from 3 to (- height 1) do
(loop for x from 0 to (- width 4) do
(loop for i from 0 to 3 do
(setf (svref buffer i)
(aref array (- y i) (+ x i))))
(if (> (product buffer) largest)
(progn
(setf largest (product buffer))))))
largest))
(defun largest-diagonal-left-up (array)
(let ((largest 0)
(buffer (make-array 4))
(height (array-dimension array 0))
(width (array-dimension array 1)))
(loop for y from 3 to (- height 1) do
(loop for x from 3 to (- width 1) do
(loop for i from 0 to 3 do
(setf (svref buffer i)
(aref array (- y i) (- x i))))
(if (> (product buffer) largest)
(progn
(setf largest (product buffer))))))
largest))
(defun largest-list (vector)
(let ((lgst 0))
(loop for i in vector do
(if (< lgst i)
(setf lgst i)))
lgst))
(defun largest-product ()
(let ((values (create-array))
(largest nil))
(setf largest (append largest (cons (largest-horizontal-forward values) nil)))
(setf largest (append largest (cons (largest-horizontal-back values) nil)))
(setf largest (append largest (cons (largest-vertical-down values) nil)))
(setf largest (append largest (cons (largest-vertical-up values) nil)))
(setf largest (append largest (cons (largest-diagonal-left-up values) nil)))
(setf largest (append largest (cons (largest-diagonal-left-down values) nil)))
(setf largest (append largest (cons (largest-diagonal-right-up values) nil)))
(setf largest (append largest (cons (largest-diagonal-right-down values) nil)))
(largest-list largest)))