-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.py
62 lines (43 loc) · 1.34 KB
/
day3.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
# Advent of Code 2024, Day 3
# (c) blu3r4y
import re
from aocd.models import Puzzle
from funcy import print_calls, print_durations
@print_calls
@print_durations(unit="ms")
def part1(data):
muls, _, _ = data
return sum(a * b for a, b in muls.values())
@print_calls
@print_durations(unit="ms")
def part2(data):
muls, dos, donts = data
limit = max(muls.keys() | dos | donts)
result, disabled = 0, False
for i in range(limit + 1):
if i in muls:
if not disabled:
a, b = muls[i]
result += a * b
elif i in dos:
disabled = False
elif i in donts:
disabled = True
return result
def load(data):
muls, dos, donts = dict(), set(), set()
for match in re.compile(r"mul\((\d{1,3}),(\d{1,3})\)").finditer(data):
muls[match.start(0)] = tuple(map(int, match.groups()))
for match in re.compile(r"do\(\)").finditer(data):
dos.add(match.start(0))
for match in re.compile(r"don't\(\)").finditer(data):
donts.add(match.start(0))
return muls, dos, donts
if __name__ == "__main__":
puzzle = Puzzle(year=2024, day=3)
ans1 = part1(load(puzzle.input_data))
assert ans1 == 190604937
puzzle.answer_a = ans1
ans2 = part2(load(puzzle.input_data))
assert ans2 == 82857512
puzzle.answer_b = ans2