-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_a.py
executable file
·135 lines (111 loc) · 3.68 KB
/
part_a.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
#!/usr/bin/env python3
from collections import defaultdict
import utils
from year_2019.day_05.part_a import InsufficientInputError
from year_2019.day_05.part_b import get_program_result_and_output_extended
DIRECTION_UP = 'up'
DIRECTION_DOWN = 'down'
DIRECTION_RIGHT = 'right'
DIRECTION_LEFT = 'left'
OFFSET_MAP = {
DIRECTION_UP: (0, -1),
DIRECTION_DOWN: (0, 1),
DIRECTION_LEFT: (-1, 0),
DIRECTION_RIGHT: (1, 0),
}
ROTATION_CW = 'cw'
ROTATION_CCW = 'ccw'
ROTATION_MAP = {
(DIRECTION_UP, ROTATION_CW): DIRECTION_RIGHT,
(DIRECTION_UP, ROTATION_CCW): DIRECTION_LEFT,
(DIRECTION_DOWN, ROTATION_CW): DIRECTION_LEFT,
(DIRECTION_DOWN, ROTATION_CCW): DIRECTION_RIGHT,
(DIRECTION_LEFT, ROTATION_CW): DIRECTION_UP,
(DIRECTION_LEFT, ROTATION_CCW): DIRECTION_DOWN,
(DIRECTION_RIGHT, ROTATION_CW): DIRECTION_DOWN,
(DIRECTION_RIGHT, ROTATION_CCW): DIRECTION_UP,
}
COMMAND_TURN_LEFT = 0
COMMAND_TURN_RIGHT = 1
class Challenge(utils.BaseChallenge):
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
1785
"""
paint_map = paint_panels(_input)
return len(paint_map)
def paint_panels(program_text, initial_paint=None):
position = (0, 0)
direction = DIRECTION_UP
paint_map = defaultdict(lambda: 0)
if initial_paint:
paint_map.update(initial_paint)
error = None
input_stream = []
while True:
paint = paint_map[position]
input_stream.append(paint)
# print(
# f"In {position}, facing {direction}, paint is {paint}, input is "
# f"{input_stream}, counter is "
# f"{error.input_stream_counter if error else 0}")
try:
_, output = get_program_result_and_output_extended(
program_text, input_stream, error=error)
# print("Finished")
break
except InsufficientInputError as e:
error = e
new_output = error.output_stream
# print(new_output)
if len(new_output) != 2:
raise Exception(
f"Expected 2 new outputs, but got {new_output}")
new_paint, command = new_output
if new_paint not in [0, 1]:
raise Exception(
f"Expected paint to be 0 or 1, but was '{new_paint}'")
paint_map[position] = new_paint
if command == COMMAND_TURN_LEFT:
rotation = ROTATION_CCW
elif command == COMMAND_TURN_RIGHT:
rotation = ROTATION_CW
else:
raise Exception(f"Unknown rotation command '{command}'")
direction = rotate(direction, rotation)
position = move(position, direction)
# paint = paint_map[position]
# print(f"In {position}, facing {direction}, paint is {paint}")
return paint_map
def rotate(direction, rotation):
"""
>>> rotate(DIRECTION_UP, ROTATION_CW)
'right'
>>> rotate(rotate(DIRECTION_UP, ROTATION_CW), ROTATION_CW)
'down'
>>> rotate(rotate(rotate(\
DIRECTION_UP, ROTATION_CW), ROTATION_CW), ROTATION_CW)
'left'
>>> rotate(rotate(rotate(rotate(\
DIRECTION_UP, ROTATION_CW), ROTATION_CW), ROTATION_CW), ROTATION_CW)
'up'
>>> rotate(rotate(rotate(rotate(\
DIRECTION_UP, ROTATION_CW), ROTATION_CCW), ROTATION_CW), ROTATION_CCW)
'up'
"""
return ROTATION_MAP[(direction, rotation)]
def move(position, direction):
"""
>>> move((0, 0), DIRECTION_UP)
(0, -1)
>>> move((0, -1), DIRECTION_DOWN)
(0, 0)
>>> move((0, -1), DIRECTION_LEFT)
(-1, -1)
"""
offset_x, offset_y = OFFSET_MAP[direction]
x, y = position
return x + offset_x, y + offset_y
Challenge.main()
challenge = Challenge()