-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd06.py
More file actions
66 lines (50 loc) · 1.76 KB
/
d06.py
File metadata and controls
66 lines (50 loc) · 1.76 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
#!/usr/bin/env python3
import sys
from collections import namedtuple
Coords = namedtuple("Coords", "x y")
Rect = namedtuple("Rect", "op lb rt")
def part1(inp):
lights = [[False] * 1000 for _ in range(1000)]
def apply_op(op, x, y):
if op == "toggle":
lights[x][y] = not lights[x][y]
if op == "turn off":
lights[x][y] = False
if op == "turn on":
lights[x][y] = True
for line in inp:
line = line.replace("through ", "")
op, lb, rt = line.rsplit(maxsplit=2)
lb = Coords(*map(int, lb.split(",")))
rt = Coords(*map(int, rt.split(",")))
rect = Rect(op, lb, rt)
for i in range(rect.lb.x, rect.rt.x + 1):
for j in range(rect.lb.y, rect.rt.y + 1):
apply_op(rect.op, i, j)
result = sum([sum(map(int, row)) for row in lights])
print(result)
def part2(inp):
lights = [[False] * 1000 for _ in range(1000)]
def apply_op(op, x, y):
if op == "toggle":
lights[x][y] = lights[x][y] + 2
if op == "turn off":
lights[x][y] = max(lights[x][y] - 1, 0)
if op == "turn on":
lights[x][y] = lights[x][y] + 1
for line in inp:
line = line.replace("through ", "")
op, lb, rt = line.rsplit(maxsplit=2)
lb = Coords(*map(int, lb.split(",")))
rt = Coords(*map(int, rt.split(",")))
rect = Rect(op, lb, rt)
for i in range(rect.lb.x, rect.rt.x + 1):
for j in range(rect.lb.y, rect.rt.y + 1):
apply_op(rect.op, i, j)
result = sum([sum(row) for row in lights])
print(result)
if __name__ == "__main__":
lines = list(filter(bool, [line.strip() for line in sys.stdin]))
part1(lines)
print()
part2(lines)