-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc2022_day10.py
75 lines (53 loc) · 2.02 KB
/
aoc2022_day10.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
"""
Advent of Code 2022
Day 10: Cathode-Ray Tube
"""
import pytest
INTERESTING = [20, 60, 100, 140, 180, 220]
OPERATIONS = {"noop": (1, lambda: 0), "addx": (2, lambda v: v)}
def parse_input(file_name):
with open(file_name, "r", encoding="ascii") as data_file:
return data_file.read().splitlines()
def trace_execution(data):
x = 1
signal = [] # signal[i] is the value of register x during cycle i+1
for line in data:
op, *params = line.split()
cycles, func = OPERATIONS[op]
signal.extend([x] * cycles) # *during* execution x is not modified
x += func(*map(int, params)) # x is updated after all cycles are completed
return signal
def chunker(seq, size):
return (seq[pos : pos + size] for pos in range(0, len(seq), size))
def day10_part1(data):
signal = trace_execution(data)
return sum(i * signal[i - 1] for i in INTERESTING)
def day10_part2(data):
signal = trace_execution(data)
return "\n".join(
"".join("#" if abs(x - i) <= 1 else "." for i, x in enumerate(crt_row))
for crt_row in chunker(signal, 40)
)
@pytest.fixture(autouse=True, name="test_data")
def fixture_test_data():
return parse_input("data/day10_test.txt")
def test_day10_part1(test_data):
assert day10_part1(test_data) == 13140
def test_day10_part2(test_data):
expected = "\n".join(
[
"##..##..##..##..##..##..##..##..##..##..",
"###...###...###...###...###...###...###.",
"####....####....####....####....####....",
"#####.....#####.....#####.....#####.....",
"######......######......######......####",
"#######.......#######.......#######.....",
]
)
assert day10_part2(test_data) == expected
if __name__ == "__main__":
input_data = parse_input("data/day10.txt")
print("Day 10 Part 1:")
print(day10_part1(input_data)) # Correct answer is 15680
print("Day 10 Part 2:")
print(day10_part2(input_data)) # Correct answer displays ZFBFHGUP