-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-10.lisp
195 lines (178 loc) · 6.38 KB
/
day-10.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
;;;; Advent of code day 10
(in-package #:aoc2019)
(defparameter *input-day-10*
"#.#................#..............#......#......
.......##..#..#....#.#.....##...#.........#.#...
.#...............#....#.##......................
......#..####.........#....#.......#..#.....#...
.....#............#......#................#.#...
....##...#.#.#.#.............#..#.#.......#.....
..#.#.........#....#..#.#.........####..........
....#...#.#...####..#..#..#.....#...............
.............#......#..........#...........#....
......#.#.........#...............#.............
..#......#..#.....##...##.....#....#.#......#...
...#.......##.........#.#..#......#........#.#..
#.............#..........#....#.#.....#.........
#......#.#................#.......#..#.#........
#..#.#.....#.....###..#.................#..#....
...............................#..........#.....
###.#.....#.....#.............#.......#....#....
.#.....#.........#.....#....#...................
........#....................#..#...............
.....#...#.##......#............#......#.....#..
..#..#..............#..#..#.##........#.........
..#.#...#.......#....##...#........#...#.#....#.
.....#.#..####...........#.##....#....#......#..
.....#..#..##...............................#...
.#....#..#......#.#............#........##...#..
.......#.....................#..#....#.....#....
#......#..###...........#.#....#......#.........
..............#..#.#...#.......#..#.#...#......#
.......#...........#.....#...#.............#.#..
..##..##.............#........#........#........
......#.............##..#.........#...#.#.#.....
#........#.........#...#.....#................#.
...#.#...........#.....#.........#......##......
..#..#...........#..........#...................
.........#..#.......................#.#.........
......#.#.#.....#...........#...............#...
......#.##...........#....#............#........
#...........##.#.#........##...........##.......
......#....#..#.......#.....#.#.......#.##......
.#....#......#..............#.......#...........
......##.#..........#..................#........
......##.##...#..#........#............#........
..#.....#.................###...#.....###.#..#..
....##...............#....#..................#..
.....#................#.#.#.......#..........#..
#........................#.##..........#....##..
.#.........#.#.#...#...#....#........#..#.......
...#..#.#......................#...............#")
(defun to-array (str)
(let ((lines)
(out))
(with-input-from-string (stream str)
(do ((line (read-line stream nil) (read-line stream nil)))
((null line))
(push line lines)))
(setf lines (nreverse lines))
(let ((x (length (first lines)))
(y (length lines)))
(setf out (make-array (list y x)))
(dotimes (j y out)
(let ((l (nth j lines)))
(dotimes (i x)
(setf (aref out j i)
(char l i))))))))
(defun angles (arr)
(let ((lenx (array-dimension arr 1))
(leny (array-dimension arr 0))
(hash (make-hash-table :test 'equal))
(out))
(dotimes (x lenx)
(dotimes (y leny)
(unless (and (zerop x) (zerop y))
(let* ((gcd (gcd x y))
(gcdx (/ x gcd))
(gcdy (/ y gcd)))
(setf (gethash (pos gcdx gcdy) hash) t)
(setf (gethash (pos gcdx (- gcdy)) hash) t)
(setf (gethash (pos (- gcdx) gcdy) hash) t)
(setf (gethash (pos (- gcdx) (- gcdy)) hash) t)))))
(maphash (lambda (k v)
(declare (ignore v))
(push k out))
hash)
out))
(defun asteroidp (arr x y)
(char= #\# (aref arr y x)))
(defun vaporize (arr x y)
(setf (aref arr y x) #\.))
(defun detect (arr pos pos-angle)
(let* ((posx (pos-x pos))
(posy (pos-y pos))
(angx (pos-x pos-angle))
(angy (pos-y pos-angle))
(maxx (array-dimension arr 1))
(maxy (array-dimension arr 0))
(max-steps (min (floor maxx (max angx 1))
(floor maxy (max angy 1)))))
(dotimes (i max-steps)
(let ((newx (+ posx (* (1+ i) angx)))
(newy (+ posy (* (1+ i) angy))))
(when (and (>= newx 0) (> maxx newx)
(>= newy 0) (> maxy newy)
(asteroidp arr newx newy))
(return-from detect t))))))
(defun count-detect (arr pos angles)
(let ((count 0))
(dolist (a angles count)
(when (detect arr pos a)
(incf count)))))
(defun max-detect (arr)
(let ((angles (angles arr))
(out)
(pos-max))
(dotimes (y (array-dimension arr 0))
(dotimes (x (array-dimension arr 1))
(when (asteroidp arr x y)
(push (list* (count-detect arr (pos x y) angles) x y) out))))
(setf pos-max (position (reduce #'max out :key #'first) out :key #'first))
(values (first (nth pos-max out))
(rest (nth pos-max out)))))
(defun try-vaporize (arr pos pos-angle)
(let* ((posx (pos-x pos))
(posy (pos-y pos))
(angx (pos-x pos-angle))
(angy (pos-y pos-angle))
(maxx (array-dimension arr 1))
(maxy (array-dimension arr 0))
(max-steps (min (floor maxx (max angx 1))
(floor maxy (max angy 1)))))
(dotimes (i max-steps)
(let ((newx (+ posx (* (1+ i) angx)))
(newy (+ posy (* (1+ i) angy))))
(when (and (>= newx 0) (> maxx newx)
(>= newy 0) (> maxy newy)
(asteroidp arr newx newy))
(vaporize arr newx newy)
(return-from try-vaporize (pos newx newy)))))))
(defun sort-angles (angles)
(let (q1 q2 q3 q4)
(dolist (a angles)
(let ((x (pos-x a))
(y (pos-y a)))
(cond ((and (>= x 0) (>= y 0)) (push a q2))
((and (>= x 0) (< y 0)) (push a q3))
((and (< x 0) (< y 0)) (push a q4))
((and (< x 0) (>= y 0)) (push a q1)))))
(nconc
(sort q1 #'< :key (lambda (a) (atan (pos-y a) (pos-x a))))
(sort q2 #'< :key (lambda (a) (atan (pos-y a) (pos-x a))))
(sort q3 #'< :key (lambda (a) (atan (pos-y a) (pos-x a))))
(sort q4 #'< :key (lambda (a) (atan (pos-y a) (pos-x a)))))))
(defun vaporize200 (arr &optional (n 200))
(let ((angles (sort-angles (angles arr)))
(count 0))
(multiple-value-bind (maxd station)
(max-detect arr)
(declare (ignore maxd))
(dotimes (i n)
(dolist (angle angles)
(let ((asteroid (try-vaporize arr station angle)))
(when asteroid
(incf count)
(when (= n count)
(return-from vaporize200
(+ (* 100 (pos-x asteroid))
(pos-y asteroid)))))))))))
(defun solution-day10-1 ()
(max-detect (to-array *input-day-10*)))
(defun solution-day10-2 ()
(vaporize200 (to-array *input-day-10*)))
(defun solve-day10 ()
(format t "Answer for puzzle 1 of day 9: ~A~&"
(solution-day10-1))
(format t "Answer for puzzle 2 of day 9: ~A~&"
(solution-day10-2)))