-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday6.py
81 lines (65 loc) · 2.39 KB
/
day6.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
import pytest
import numpy as np
from common.helpers import extract_numbers
from typing import List
TURN_ON = 0
TURN_OFF = 1
TOGGLE = 2
@pytest.mark.parametrize('data, expected',
[
([[0, 0, 999, 999, TURN_ON]], 1000000),
([[0, 0, 999, 0, TOGGLE]], 1000),
([[0, 0, 999, 999, TURN_ON], [499, 499, 500, 500, TURN_OFF]], 999996),
])
def test_part1(data: List[List[int]], expected: int):
assert part1(data) == expected
@pytest.mark.parametrize('data, expected',
[
([[0, 0, 0, 0, TURN_ON]], 1),
([[0, 0, 999, 999, TOGGLE]], 2000000),
])
def test_part2(data: List[List[int]], expected: int):
assert part2(data) == expected
def parse_input(filename: str):
instructions = []
lines = [line.strip() for line in open(filename).readlines()]
for line in lines:
digits = extract_numbers(line)
if line.startswith('turn on'):
digits.append(TURN_ON)
elif line.startswith('turn off'):
digits.append(TURN_OFF)
else:
digits.append(TOGGLE)
instructions.append(digits)
return instructions
def part1(data: List[List[int]]) -> int:
grid = np.zeros((1000, 1000))
for instruction in data:
x1, y1, x2, y2, op = instruction
if op == TURN_ON:
grid[y1: y2 + 1, x1: x2 + 1] = 1
elif op == TURN_OFF:
grid[y1: y2 + 1, x1: x2 + 1] = 0
else:
grid[y1: y2 + 1, x1: x2 + 1] = 1 - grid[y1: y2 + 1, x1: x2 + 1]
return np.count_nonzero(grid == 1)
def part2(data: List[List[int]]) -> int:
grid = np.zeros((1000, 1000))
for instruction in data:
x1, y1, x2, y2, op = instruction
if op == TURN_ON:
grid[y1: y2 + 1, x1: x2 + 1] = grid[y1: y2 + 1, x1: x2 + 1] + 1
elif op == TURN_OFF:
for y in range(y1, y2 + 1):
for x in range(x1, x2 + 1):
grid[y][x] = max(0, grid[y][x] - 1)
else:
grid[y1: y2 + 1, x1: x2 + 1] = grid[y1: y2 + 1, x1: x2 + 1] + 2
return int(np.sum(grid))
def main():
data = parse_input('input/day6.txt')
print(f'Part 1: {part1(data)}')
print(f'Part 2: {part2(data)}')
if __name__ == "__main__":
main()