-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday06_brute.py
More file actions
33 lines (28 loc) · 1.09 KB
/
Copy pathday06_brute.py
File metadata and controls
33 lines (28 loc) · 1.09 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
import re, collections
parse = lambda line: re.match(
r"(turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+)", line
).groups()
puzzle_input = open("puzzle.txt").read().split("\n")
instructions = map(parse, puzzle_input)
switched_on = set()
brightness = collections.defaultdict(int)
# TODO: optimise to not check everything via brute force
for setting, *nums in instructions:
ax, ay, bx, by = map(int, nums)
for x in range(ax, bx + 1):
for y in range(ay, by + 1):
if setting == "turn on":
switched_on.add((x, y))
brightness[(x, y)] += 1
elif setting == "turn off":
if (x, y) in switched_on:
switched_on.remove((x, y))
brightness[(x, y)] = max(brightness[(x, y)] - 1, 0)
elif setting == "toggle":
if (x, y) in switched_on:
switched_on.remove((x, y))
else:
switched_on.add((x, y))
brightness[(x, y)] += 2
print("Part 1:", len(switched_on))
print("Part 2:", sum(brightness.values()))