-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_17.py
83 lines (67 loc) · 1.97 KB
/
day_17.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
import util
from copy import copy
from intcode import computer, format_program
from os import system
def compute_alignment(video):
alignment = 0
for i in range(1, len(video) - 2):
for j in range(1, len(video[i]) - 2):
if video[i][j] == "#":
if (
video[i - 1][j] == "#"
and video[i + 1][j] == "#"
and video[i][j - 1] == "#"
and video[i][j + 1] == "#"
):
alignment += i * j
return alignment
def part1(data):
stdout = computer.Run(data)
video = []
line = []
for out in stdout:
if out == 10:
video.append(line)
line = []
else:
line.append(chr(out))
util.Answer(1, compute_alignment(video))
def part2(data):
data[0] = 2 # wake up robot
"LR8|L10", "R", "10", "L", "8"
"R", "12", "L", "10", "R", "10",
"L", "2", "R", "12", "L", "6"
# A------
"R", "12", "L", "10", "R", "12", # A
"L", "8", "R", "10", "R", "6",
"R", "12", "L", "10", "R", "12", # A
"R", "12", "L", "10", "R", "10", # A
"L", "8",
"L", "8", "R", "10", "R", "6",
"R", "12", "L", "10", "R", "10",
"L", "8",
"L", "8", "R", "10", "R", "6",
"R", "12", "L", "10", "R", "10",
program = [
"A,B,C,A,B,A,B", # program sequence (A,B,C)
"R,12,L", # program A
"10,R,12", # program B
"L,8,R,10,R,6", # program C
"n\n", # video feed
]
stdout = computer.Run(data, (ord(v) for v in "\n".join(program)))
line = []
for out in stdout:
if out == 10:
if len(line) == 0:
print("") # system('cls')
else:
print("".join(line))
line = []
else:
line.append(chr(out))
util.Answer(2, None)
if __name__ == "__main__":
data = format_program(util.ReadPuzzle())
part1(copy(data))
part2(copy(data))