-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
426 lines (365 loc) · 13 KB
/
util.py
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
from functools import partial
class Day:
def __init__(self, day: int, part: int):
from problems import description
self.day = day
self.part = part
self.desc = description(day, part)
self.task = self.desc.strip().split("\n")[-1].strip()
self.data = []
self.memory = {}
self.pointer = 0
self.rel_base = 0
self.exec_time = -1
self.debug = False
self.concurrent = False
self.input_queue = []
def __repr__(self):
return f"Day({self.day,self.part}): Pointer:{self.pointer}"
def __str__(self):
return f"Advent of Code class for Day {self.day}: Part {self.part}."
def __copy__(self):
cls = self.__class__
result = cls.__new__(cls)
result.__dict__.update(self.__dict__)
return result
def __deepcopy__(self, memo):
import copy
cls = self.__class__
result = cls.__new__(cls)
memo[id(self)] = result
for k, v in self.__dict__.items():
setattr(result, k, copy.deepcopy(v, memo))
return result
def __len__(self):
return len(self.data)
def __getitem__(self, position):
return self.data[position]
def __eq__(self, other):
if type(other) == type(self):
return self.data == other.data
else:
return self.data == other
def __add__(self, other):
if isinstance(other, list):
self.data.extend(other)
else:
self.data.append(other)
return self.data
def __call__(self):
print(self)
print(self.desc)
try:
self.hist()
except AttributeError:
pass
try:
self.answer(v=1)
except AttributeError:
pass
def copy(self, deep=False):
import copy
if deep is True:
return copy.deepcopy(self)
return copy.copy(self)
def load(self, data=None, typing=str, sep="\n", path="") -> list:
"""Loads Data for Problem
File _must_ be named dayXX.txt
Returns data and makes it available as attribte "data"
Keyword Arguments:
data {list} -- Load computed data not from file (default: {None})
typing {type} -- Type of data in list (default: {str})
sep {str} -- Separator in input data (default: {"\n"})
Returns:
list -- Data for Problem
"""
if path == "":
path = f"data/day{self.day:02d}.txt"
if data:
self.data = data
else:
with open(path) as f:
data = f.read().strip().split(sep)
self.data = list(map(typing, data))
self.raw_data = [self.data.copy()]
self.mem_load()
return self
def bake(self):
"""Finalize processed data as resettable
"""
self.raw_data.append(self.data.copy())
return self
def mem_load(self):
"""Loads data into opcode memory
"""
self.memory = {i: el for i, el in enumerate(self.data)}
return self
def mem_dump(self, extend=False):
"""Dumps opcode memory into data
Keyword Arguments:
extend {bool} -- Extend data by maximum memory (default: {False})
"""
for k, v in self.memory.items():
if k > len(self.data):
if extend is True:
self.data.extend([0] * (k - len(self.data) + 2))
else:
continue
self.data[k] = v
return self
def input(self, data):
"""Input data to queue
"""
self.input_queue.append(data)
return self
def reset(self, hist_step=None):
"""Reset Opcode class
Resets data, and pointer.
Flushes input queue.
Can restore specific data from history
Keyword Arguments:
hist_step {[int]} -- restoration point (default: {Last})
"""
self.pointer = 0
self.rel_base = 0
self.input_queue = []
self.memory = {}
if hist_step is None:
hist_step = len(self.raw_data) - 1
self.data = self.raw_data[hist_step].copy()
self.raw_data = [x.copy() for x in self.raw_data[: hist_step + 1]]
self.mem_load()
return self
def sum(self) -> float:
return sum(self.data)
def apply(self, func, *args, **kwargs) -> list:
"""Apply a function to every element.
Changes the original data.
Arguments:
func {function} -- Function to apply to every element in input
Returns:
list -- Function applied to every element in input
"""
mapfunc = partial(func, *args, **kwargs)
self.data = list(map(mapfunc, self.data))
self.mem_load()
return self
def time(self):
from time import time
if self.exec_time == -1:
self.exec_time = time()
else:
print(f"Execution time was {time()-self.exec_time:.2f} seconds.")
self.exec_time = -1
return self
def execute_opcode(self, reset_pointer=True) -> list:
"""Execute OpCode operation
1: Add
2: Multiply
3: Input
4: Output
5: Jump If True
6: Jump If False
7: Less Than
8: Equals
9: Rel Base Update
99: Exit
Returns:
list -- Opcode after execution
"""
if reset_pointer is True:
self.pointer = 0
def __opmode(pointer: int, mode: tuple, offset: int, get=False) -> int:
exec_mode = int(mode[offset - 1])
if exec_mode == 0:
# Position Mode
position = self.memory[pointer + offset]
elif exec_mode == 1:
# Immediate Mode
position = pointer + offset
elif exec_mode == 2:
# Relative Mode
position = self.memory[pointer + offset] + self.rel_base
else:
raise RuntimeError(f"ERR: \n Exec Mode: {exec_mode} not understood")
if position < 0:
raise RuntimeError(
f"ERR: \n Memory Access Error: {position} accessed. No Negative Memory Adress!"
)
if get:
return self.memory.get(position, 0)
else:
return position
def __pointer_move(instruction: int):
step_size = {
1: 4,
2: 4,
3: 2,
4: 2,
5: 3,
6: 3,
7: 4,
8: 4,
9: 2,
99: 0,
}
self.pointer += step_size[instruction]
def __instructor(code: int):
mode = f"{code:05d}"
out_pointer = self.pointer
instruct = int(mode[3:])
__pointer_move(instruct)
return instruct, (mode[2], mode[1], mode[0]), out_pointer
while True:
instruct, param, pointer = __instructor(self.memory[self.pointer])
if instruct == 1:
# Multiply
self.memory[__opmode(pointer, param, offset=3)] = __opmode(
pointer, param, offset=1, get=True
) + __opmode(pointer, param, offset=2, get=True)
elif instruct == 2:
# Add
self.memory[__opmode(pointer, param, offset=3)] = __opmode(
pointer, param, offset=1, get=True
) * __opmode(pointer, param, offset=2, get=True)
elif instruct == 3:
# Input
if not getattr(self, "input_queue"):
self.memory[__opmode(pointer, param, offset=1)] = int(input("Provide input: "))
elif isinstance(self.input_queue, int):
self.memory[__opmode(pointer, param, offset=1)] = self.input_queue
elif isinstance(self.input_queue, list):
self.memory[__opmode(pointer, param, offset=1)] = int(self.input_queue.pop(0))
elif instruct == 4:
# Output
self.result = self.diagnostic = __opmode(pointer, param, offset=1, get=True)
if self.debug is True:
print(self.diagnostic)
if self.concurrent is True:
return self.diagnostic
elif instruct == 5:
# Jump If True
if __opmode(pointer, param, offset=1, get=True) != 0:
self.pointer = __opmode(pointer, param, offset=2, get=True)
elif instruct == 6:
# Jump If False
if __opmode(pointer, param, offset=1, get=True) == 0:
self.pointer = __opmode(pointer, param, offset=2, get=True)
elif instruct == 7:
# Less Than
self.memory[__opmode(pointer, param, offset=3)] = int(
__opmode(pointer, param, offset=1, get=True)
< __opmode(pointer, param, offset=2, get=True)
)
elif instruct == 8:
# Equals
self.memory[__opmode(pointer, param, offset=3)] = int(
__opmode(pointer, param, offset=1, get=True)
== __opmode(pointer, param, offset=2, get=True)
)
elif instruct == 9:
# Relative Base Adjust
self.rel_base += int(__opmode(pointer, param, offset=1, get=True))
elif instruct == 99:
self.input_queue = [] # Flush inputs
#self.mem_dump(extend=True)
return self
else:
raise RuntimeError(
f"ERR {instruct}: \n Data Dump: {self.memory[pointer]} Index:{pointer}"
)
break
def hist(self):
"""Produce data history
"""
length = len(self.raw_data)
ends = ("y", "ies")
print(f"{length} histor{ends[length != 1]} saved")
print("=" * 15)
for hist in self.raw_data:
s = f"{hist}"
print(s[:70] + " . . ." * (70 < len(s)))
def summary(self):
"""Produce Task Summary
"""
s = f"The problem for Day {self.day} and part {self.part}:\n{self.task}"
if hasattr(self, "result"):
print(s)
self.answer(v=True)
else:
print(s)
def answer(self, num=None, v=False) -> str:
"""Produce answer string
Saves number in result attribute and returns a nice string
Keyword Arguments:
num {int} -- Input result (default: {None})
v {bool} -- Verbesity (default: {False})
Returns:
str -- Answer string
"""
if num is not None:
self.result = num
s = f"The Solution on Day {self.day} for Part {self.part} is: {self.result}"
if v:
print(s)
return s
class Robot(Day):
def __init__(self, day, part):
super().__init__(day, part)
self.concurrent = True
self.location = (0, 0)
self.path = {self.location: 0}
self.direction = 0
self.painted = set()
def parse_direction(self):
dir_dict = {0: (0, 1), # Up
1: (1, 0), # Right
2: (0, -1), # Down
3: (-1, 0)} # Left
return dir_dict[self.direction]
def turn(self, direction):
# left 0 right 1
if direction == 0:
self.direction = (self.direction - 1) % 4
elif direction == 1:
self.direction = (self.direction + 1) % 4
return self.walk()
def walk(self):
step_x, step_y = self.parse_direction()
self.location = self.location[0] + step_x, self.location[1] + step_y
self.path[self.location] = self.path.get(self.location, 0)
return self
def vision(self):
self.input(self.path[self.location])
return self
def paint(self, color):
self.path[self.location] = color
self.painted.add(self.location)
# black 0 white 1
return self
def run(self):
while True:
# First See
self.vision()
# Then think and pause
out = self.execute_opcode(reset_pointer=False)
if isinstance(out, Robot):
break
# Then paint
self.paint(out)
# Then think and pause
out = self.execute_opcode(reset_pointer=False)
# Then turn
self.turn(out)
return self
def visualize(self):
x, y = zip(*self.path.keys())
painting = {0: ".", 1: "#"}
for j in range(max(y)+1, min(y)-2, -1):
for i in range(min(x), max(x)+1):
print(painting[self.path.get((i, j), 0)], end=" ")
print()
return self
if __name__ == "__main__":
day = Day(1, 1)
day()