-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17.el
239 lines (218 loc) · 16 KB
/
day17.el
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
;; -*- lexical-binding: t -*-
(setq problem1 "1,330,331,332,109,3974,1102,1182,1,15,1101,1475,0,24,1001,0,0,570,1006,570,36,101,0,571,0,1001,570,-1,570,1001,24,1,24,1106,0,18,1008,571,0,571,1001,15,1,15,1008,15,1475,570,1006,570,14,21102,1,58,0,1105,1,786,1006,332,62,99,21101,333,0,1,21102,73,1,0,1105,1,579,1102,0,1,572,1101,0,0,573,3,574,101,1,573,573,1007,574,65,570,1005,570,151,107,67,574,570,1005,570,151,1001,574,-64,574,1002,574,-1,574,1001,572,1,572,1007,572,11,570,1006,570,165,101,1182,572,127,1002,574,1,0,3,574,101,1,573,573,1008,574,10,570,1005,570,189,1008,574,44,570,1006,570,158,1105,1,81,21102,340,1,1,1105,1,177,21101,0,477,1,1106,0,177,21102,1,514,1,21101,0,176,0,1106,0,579,99,21102,184,1,0,1105,1,579,4,574,104,10,99,1007,573,22,570,1006,570,165,1002,572,1,1182,21101,0,375,1,21101,0,211,0,1105,1,579,21101,1182,11,1,21102,1,222,0,1106,0,979,21102,1,388,1,21102,1,233,0,1106,0,579,21101,1182,22,1,21101,0,244,0,1105,1,979,21101,0,401,1,21102,1,255,0,1106,0,579,21101,1182,33,1,21102,1,266,0,1106,0,979,21102,414,1,1,21102,277,1,0,1105,1,579,3,575,1008,575,89,570,1008,575,121,575,1,575,570,575,3,574,1008,574,10,570,1006,570,291,104,10,21102,1182,1,1,21102,1,313,0,1106,0,622,1005,575,327,1101,1,0,575,21101,327,0,0,1105,1,786,4,438,99,0,1,1,6,77,97,105,110,58,10,33,10,69,120,112,101,99,116,101,100,32,102,117,110,99,116,105,111,110,32,110,97,109,101,32,98,117,116,32,103,111,116,58,32,0,12,70,117,110,99,116,105,111,110,32,65,58,10,12,70,117,110,99,116,105,111,110,32,66,58,10,12,70,117,110,99,116,105,111,110,32,67,58,10,23,67,111,110,116,105,110,117,111,117,115,32,118,105,100,101,111,32,102,101,101,100,63,10,0,37,10,69,120,112,101,99,116,101,100,32,82,44,32,76,44,32,111,114,32,100,105,115,116,97,110,99,101,32,98,117,116,32,103,111,116,58,32,36,10,69,120,112,101,99,116,101,100,32,99,111,109,109,97,32,111,114,32,110,101,119,108,105,110,101,32,98,117,116,32,103,111,116,58,32,43,10,68,101,102,105,110,105,116,105,111,110,115,32,109,97,121,32,98,101,32,97,116,32,109,111,115,116,32,50,48,32,99,104,97,114,97,99,116,101,114,115,33,10,94,62,118,60,0,1,0,-1,-1,0,1,0,0,0,0,0,0,1,50,26,0,109,4,1201,-3,0,587,20102,1,0,-1,22101,1,-3,-3,21102,0,1,-2,2208,-2,-1,570,1005,570,617,2201,-3,-2,609,4,0,21201,-2,1,-2,1106,0,597,109,-4,2106,0,0,109,5,2101,0,-4,630,20101,0,0,-2,22101,1,-4,-4,21101,0,0,-3,2208,-3,-2,570,1005,570,781,2201,-4,-3,653,20101,0,0,-1,1208,-1,-4,570,1005,570,709,1208,-1,-5,570,1005,570,734,1207,-1,0,570,1005,570,759,1206,-1,774,1001,578,562,684,1,0,576,576,1001,578,566,692,1,0,577,577,21102,702,1,0,1106,0,786,21201,-1,-1,-1,1106,0,676,1001,578,1,578,1008,578,4,570,1006,570,724,1001,578,-4,578,21102,731,1,0,1105,1,786,1106,0,774,1001,578,-1,578,1008,578,-1,570,1006,570,749,1001,578,4,578,21101,756,0,0,1105,1,786,1106,0,774,21202,-1,-11,1,22101,1182,1,1,21102,774,1,0,1105,1,622,21201,-3,1,-3,1106,0,640,109,-5,2105,1,0,109,7,1005,575,802,20102,1,576,-6,20101,0,577,-5,1105,1,814,21101,0,0,-1,21101,0,0,-5,21101,0,0,-6,20208,-6,576,-2,208,-5,577,570,22002,570,-2,-2,21202,-5,51,-3,22201,-6,-3,-3,22101,1475,-3,-3,2102,1,-3,843,1005,0,863,21202,-2,42,-4,22101,46,-4,-4,1206,-2,924,21102,1,1,-1,1105,1,924,1205,-2,873,21102,1,35,-4,1106,0,924,1202,-3,1,878,1008,0,1,570,1006,570,916,1001,374,1,374,1201,-3,0,895,1101,0,2,0,2101,0,-3,902,1001,438,0,438,2202,-6,-5,570,1,570,374,570,1,570,438,438,1001,578,558,921,21002,0,1,-4,1006,575,959,204,-4,22101,1,-6,-6,1208,-6,51,570,1006,570,814,104,10,22101,1,-5,-5,1208,-5,49,570,1006,570,810,104,10,1206,-1,974,99,1206,-1,974,1102,1,1,575,21101,973,0,0,1105,1,786,99,109,-7,2105,1,0,109,6,21101,0,0,-4,21102,0,1,-3,203,-2,22101,1,-3,-3,21208,-2,82,-1,1205,-1,1030,21208,-2,76,-1,1205,-1,1037,21207,-2,48,-1,1205,-1,1124,22107,57,-2,-1,1205,-1,1124,21201,-2,-48,-2,1105,1,1041,21102,1,-4,-2,1106,0,1041,21101,0,-5,-2,21201,-4,1,-4,21207,-4,11,-1,1206,-1,1138,2201,-5,-4,1059,2102,1,-2,0,203,-2,22101,1,-3,-3,21207,-2,48,-1,1205,-1,1107,22107,57,-2,-1,1205,-1,1107,21201,-2,-48,-2,2201,-5,-4,1090,20102,10,0,-1,22201,-2,-1,-2,2201,-5,-4,1103,2102,1,-2,0,1106,0,1060,21208,-2,10,-1,1205,-1,1162,21208,-2,44,-1,1206,-1,1131,1106,0,989,21101,0,439,1,1105,1,1150,21101,477,0,1,1106,0,1150,21102,1,514,1,21102,1,1149,0,1105,1,579,99,21101,1157,0,0,1106,0,579,204,-2,104,10,99,21207,-3,22,-1,1206,-1,1138,2101,0,-5,1176,2102,1,-4,0,109,-6,2106,0,0,32,5,46,1,3,1,46,1,3,1,46,1,3,1,46,1,3,1,46,1,3,1,40,11,40,1,5,1,44,1,5,1,44,1,5,1,44,1,5,1,44,1,5,1,40,11,40,1,3,1,46,1,3,1,46,1,3,1,46,1,3,9,38,1,11,1,26,1,11,1,11,1,26,1,11,1,11,1,26,1,11,5,7,1,26,1,15,1,7,1,26,1,15,1,7,1,26,1,15,1,7,1,26,1,11,7,5,1,26,1,11,1,3,1,1,1,5,1,26,1,9,13,1,1,3,13,10,1,9,1,1,1,3,1,1,1,3,1,1,1,3,1,22,5,5,1,1,13,3,1,26,1,5,1,5,1,1,1,3,1,5,1,26,1,5,1,5,7,5,1,26,1,5,1,7,1,9,1,26,1,5,1,7,1,9,1,26,1,5,1,7,1,9,1,26,1,5,1,7,11,26,1,5,1,34,11,5,1,34,1,15,1,34,1,5,9,1,9,26,1,5,1,7,1,9,1,26,1,5,1,7,1,9,1,26,1,5,1,7,1,9,1,22,11,7,1,9,1,22,1,3,1,13,1,9,1,22,1,3,1,13,1,9,1,22,1,3,1,13,1,9,1,22,1,3,1,13,1,9,1,22,1,3,1,13,1,9,1,22,5,13,11,22")
;; a program in memory is a map of (location -> data)
(defun make-program (program-string)
(let ((memory (make-hash-table))
(data-list (mapcar 'string-to-number (split-string program-string ","))))
(cl-loop
for data in data-list
for loc from 0 to (length data-list)
do (puthash loc data memory))
memory))
(defun decode-inst (inst-raw)
(let ((inst (mod inst-raw 100))
(op1 (mod (/ inst-raw 100) 10))
(op2 (mod (/ inst-raw 1000) 10))
(op3 (mod (/ inst-raw 10000) 10))
)
(list inst op1 op2 op3)))
;; a computer is
;; - a program
;; - a program counter
;; - an ascii bot
(defun make-computer (program)
(let ((amp (make-hash-table)))
(puthash 'program program amp)
(puthash 'pc 0 amp)
(puthash 'rel-base 0 amp)
(puthash 'bot (make-ascii-bot) amp)
amp))
;; run a computer until it produces output or halts
(defun run-computer (computer)
(let ((program (gethash 'program computer))
done)
(defun prog-get (addr) (gethash addr program 0))
(defun prog-set (addr val) (puthash addr val program))
(defun prog-set-output (raw-addr val mode)
;; (print (concat "prog-set-output: " (prin1-to-string (list raw-addr val mode))))
(let ((addr
(+ (prog-get raw-addr)
(if (= mode 0)
0
(prog-rel-base)))))
(puthash addr val program)))
(defun prog-pc () (gethash 'pc computer))
(defun prog-rel-base () (gethash 'rel-base computer))
(defun prog-bot () (gethash 'bot computer))
(defun get-operand (val mode)
;; (print (prin1-to-string (list (prog-pc) val mode)))
(cond ((= mode 0) (prog-get val))
((= mode 1) val)
((= mode 2) (prog-get (+ val (prog-rel-base))))
(t (concat "Unknown mode: " (number-to-string mode)))))
(defun op (f arg1 arg2 out-mode)
(prog-set-output (+ (prog-pc) 3) (apply f (list arg1 arg2)) out-mode)
(list (+ 4 (prog-pc))))
(defun op-test (f arg1 arg2 out-mode)
(prog-set-output (+ (prog-pc) 3) (if (apply f (list arg1 arg2)) 1 0) out-mode)
(list (+ 4 (prog-pc))))
(defun op-input (out-mode)
(let ((val (bot-get (prog-bot))))
;; (print (concat "op-input: " (prin1-to-string (list val out-mode))))
(prog-set-output (+ (prog-pc) 1) val out-mode)
(list (+ 2 (prog-pc)))))
(defun run-one-op ()
(let* ((pc (prog-pc))
(inst-parsed (decode-inst (prog-get pc)))
(inst (car inst-parsed))
(arg1 (get-operand (prog-get (+ pc 1)) (cadr inst-parsed)))
(arg2 (get-operand (prog-get (+ pc 2)) (caddr inst-parsed))))
;; (print (concat "pc: " (number-to-string pc) " inst: " (prin1-to-string inst-parsed) " arg1: " (prin1-to-string arg1) " arg2: " (prin1-to-string arg2)))
;; Run an op and return (<next-pc> . <output>)
(cond ((= inst 1) (op '+ arg1 arg2 (cadddr inst-parsed)))
((= inst 2) (op '* arg1 arg2 (cadddr inst-parsed)))
((= inst 3) (op-input (cadr inst-parsed)))
((= inst 4)
(bot-put (prog-bot) arg1)
(list (+ 2 pc)))
((= inst 5) (list (if (not (= 0 arg1)) arg2 (+ 3 pc))))
((= inst 6) (list (if (= 0 arg1) arg2 (+ 3 pc))))
((= inst 7) (op-test '< arg1 arg2 (cadddr inst-parsed)))
((= inst 8) (op-test '= arg1 arg2 (cadddr inst-parsed)))
((= inst 9)
;; (print (concat "rel-base: " (number-to-string (prog-rel-base))))
(puthash 'rel-base (+ (prog-rel-base) arg1) computer)
(list (+ 2 pc)))
((= inst 99)
(cons 'done 'done))
(t (print (concat "Unknown instruction: " (number-to-string inst) " at pc " (number-to-string pc)))))))
(while (not done)
(let ((result (run-one-op)))
(puthash 'pc (car result) computer)
;; run until output appears, or the program exits
(if (cdr result)
(setq done (cdr result)))))
(prog-bot)))
;; ascii bot
;; 'tiles map (<pos> . <val>)
;; 'position
(defun make-ascii-bot ()
(let ((bot (make-hash-table)))
(puthash 'tiles (make-hash-table :test 'equal) bot)
(puthash 'pos (cons 0 0) bot)
bot))
(defun bot-put (bot input)
(let ((pos (gethash 'pos bot)))
(cond ((= input ?\n)
(puthash 'pos (cons 0 (1+ (cdr pos))) bot))
(t
(puthash pos input (gethash 'tiles bot))
(puthash 'pos (cons (1+ (car pos)) (cdr pos)) bot)))))
(defun bot-get (bot)
(gethash 'move-dir bot))
(setq computer (make-computer (make-program problem1)))
(setq result (run-computer computer))
(defun get-tiles-size (bot)
(let ((min-x 1000)
(max-x -1)
(min-y 1000)
(max-y -1))
(maphash (lambda (key value)
(setq min-x (min min-x (car key)))
(setq max-x (max max-x (car key)))
(setq min-y (min min-y (cdr key)))
(setq max-y (max max-y (cdr key))))
(gethash 'tiles bot))
(list min-x min-y max-x max-y)))
(get-tiles-size result)
(0 0 50 48)
(setq tiles (gethash 'tiles result))
(defun bot-next-pos (pos dir)
(let ((x (car pos))
(y (cdr pos)))
(cond ((= dir 1) (cons x (1+ y)))
((= dir 2) (cons x (1- y)))
((= dir 3) (cons (1- x) y))
((= dir 4) (cons (1+ x) y)))))
(defun is-intersection (pos tiles)
(defun get-at (pos dir)
(gethash (bot-next-pos pos dir) tiles))
(and
(eq (gethash pos tiles) ?#)
(eq (get-at pos 1) ?#)
(eq (get-at pos 2) ?#)
(eq (get-at pos 3) ?#)
(eq (get-at pos 4) ?#)))
(defun validate-intersection (bot)
(let ((tiles (gethash 'tiles bot))
(alignments 0))
(cl-loop
for x from 0 to 50 do
(cl-loop
for y from 0 to 48 do
(let ((pos (cons x y)))
(if (is-intersection pos tiles)
(setq alignments (+ alignments (* x y)))))))
alignments))
(validate-intersection result)
;; => 6520
(cl-loop
for x from 0 to 50 do
(cl-loop
for y from 0 to 48 do
(let ((char (gethash (cons x y) tiles)))
(if char
(princ (concat (string char) (string char)))
(princ " ")))
finally (princ "\n")))
....................................................................................##############
....................................................................................##..........##
....................................................................................##..........##
....................................................................................##..........##
........................................................................##########################
........................................................................##..........##............
........................................................................##..........##............
........................................................................##..........##............
........................................................................##..........##............
........................................................................##..........##............
....................................######################..............##..##########............
........................................................##..............##..##....................
........................................................##..............##..##....................
........................................................##..............##..##....................
........................................................##################..##....................
............................................................................##....................
............................................................................##....................
............................................................................##....................
............................................................................######################
................................................................................................##
....................................................##########################..................##
....................................................##......................##..................##
........................##################......##########..................##..................##
........................##..............##......##..##..##..................##..................##
........................##..............##......##..##..##..................##..................##
........................##..............##......##..##..##..................##..................##
............######################......######################..............##..................##
............##..........##......##..............##..##..##..##..............##..................##
............##..........##......##..............######################......######################
............##..........##......##..................##..##..##......##............................
............##..........##......##..................##..##..##......##............................
............##..........##......##..................##..##..##......##............................
##########################......##..................##########......##............................
##..........##..................##......................##..........##............................
##..........##..................##########################..........##............................
##..........##......................................................##............................
##############......................................................##............................
....................................................................##............................
....................................................##################............................
....................................................##............................................
....................................................##............................................
....................................................##............................................
....................................................##............................................
....................................................##............................................
....................................................##............................................
....................................................##............................................
....................................................##............................................
....................................................##............................................
....................................................##............................................
....................................................##............................................
....................................................^^............................................