-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_11.py
More file actions
136 lines (114 loc) · 4.19 KB
/
day_11.py
File metadata and controls
136 lines (114 loc) · 4.19 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
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
# %% Integer opcode computer
from __future__ import annotations
from collections import defaultdict
from itertools import tee, islice
from typing import Iterable, List
from dataclasses import dataclass
import matplotlib.pyplot as plt
import numpy as np
@dataclass
class Point:
x: int
y: int
def distance_to(self, p: Point) -> int:
return abs(p.x - self.x) + abs(p.y - self.y)
def __iter__(self):
yield self.y
yield self.x
def __add__(self, other: Point) -> Point:
return self.__class__(self.x + other.x, self.y + other.y)
def __hash__(self):
return hash((self.x, self.y))
def get_parameters(seq, pointer, opmodes, number, base):
result = []
for i in range(number):
if opmodes[i] == 0:
result.append(seq[seq[pointer + i + 1]])
elif opmodes[i] == 1:
result.append(seq[pointer + i + 1])
elif opmodes[i] == 2:
result.append(seq[seq[pointer + i + 1] + base])
else:
print("invalid opmode")
return result
def run_program(program_code: List[int], program_input: Iterable[int]) -> Iterable[int]:
increments = {1: 4, 2: 4, 3: 2, 4: 2, 5: 3, 6: 3, 7: 4, 8: 4, 9: 2}
input_iter = iter(program_input)
pointer = 0
base = 0
while (opcode := program_code[pointer] % 100) != 99:
opmodes = [program_code[pointer] // 10 ** n % 10 for n in range(2, 5)]
if opcode == 1 or opcode == 2:
op1, op2 = get_parameters(program_code, pointer, opmodes, 2, base)
idx = program_code[pointer + 3] + (base if opmodes[2] else 0)
program_code[idx] = op1 + op2 if opcode == 1 else op1 * op2
elif opcode == 3:
idx = program_code[pointer + 1] + (base if opmodes[0] else 0)
program_code[idx] = next(input_iter)
elif opcode == 4:
out = get_parameters(program_code, pointer, opmodes, 1, base)[0]
yield out
elif opcode == 5 or opcode == 6:
op1, op2 = get_parameters(program_code, pointer, opmodes, 2, base)
switch = bool(op1) if opcode == 5 else not bool(op1)
if switch:
pointer = op2
continue
elif opcode == 7 or opcode == 8:
op1, op2 = get_parameters(program_code, pointer, opmodes, 2, base)
switch = op1 < op2 if opcode == 7 else op1 == op2
idx = program_code[pointer + 3] + (base if opmodes[2] else 0)
program_code[idx] = 1 if switch else 0
elif opcode == 9:
op = get_parameters(program_code, pointer, opmodes, 1, base)[0]
base += op
else:
print("Unknown opcode")
pointer += increments[opcode]
with open("day_11.input", "r") as input_data:
program = defaultdict(
int, {i: int(x) for i, x in enumerate(input_data.readline().split(","))}
)
def run_robot(robot_program, init):
def input_generator(region, init_value):
yield init_value
while True:
yield region.get(pos, 0)
direction = "U"
pos = Point(0, 0)
region_map = {}
directions = {
("U", 0): "L",
("U", 1): "R",
("L", 0): "D",
("R", 1): "D",
("D", 0): "R",
("D", 1): "L",
("R", 0): "U",
("L", 1): "U",
}
delta_move = {
"U": Point(0, -1),
"L": Point(-1, 0),
"D": Point(0, 1),
"R": Point(1, 0),
}
a, b = tee(run_program(robot_program, input_generator(region_map, init)))
next(b, None)
for color, move in islice(zip(a, b), None, None, 2):
region_map[pos] = color
direction = directions[(direction, move)]
pos += delta_move[direction]
points = np.array([list(p) for p, value in region_map.items() if value])
points -= np.min(points, axis=0)
reg = np.zeros(np.max(points, axis=0) + 1)
reg[points[:, 0], points[:, 1]] = 1
return len(region_map), reg
num_paints, registration = run_robot(program.copy(), 0)
print(f"Part 1: Number of painted fields is {num_paints}")
plt.imshow(registration)
plt.show()
num_paints, registration = run_robot(program.copy(), 1)
print(f"Part 2: Number of painted fields is {num_paints}")
plt.imshow(registration)
plt.show()