-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday7.py
More file actions
executable file
·103 lines (66 loc) · 2.26 KB
/
Copy pathday7.py
File metadata and controls
executable file
·103 lines (66 loc) · 2.26 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from math import log10
import operator
from aoc_utils import * # type: ignore
from aocd import get_data
data = get_data(year=2024, day=7, block=True)
def operator_combinations(length, operators):
if length == 1:
for op in operators:
yield [op]
else:
for combo in operator_combinations(length - 1, operators):
for op in operators:
yield [op] + combo
def calibration(equations, operators):
s = 0
for test, initial, *values in equations:
posts = len(values)
valid = False
for combo in operator_combinations(posts, operators):
value = initial
for a, op in zip(values, combo):
value = op(value, a)
if value > test:
break
if value == test:
valid = True
break
if valid:
s += test
return s
def part_one(data):
equations = [ints(line) for line in data.splitlines()]
return calibration(equations, [operator.add, operator.mul])
def concat(a, b):
return a * (10 ** (int(log10(b)) + 1)) + b
def part_two(data):
equations = [ints(line) for line in data.splitlines()]
return calibration(equations, [operator.add, operator.mul, concat])
# Alternate implementations that are much faster
# I believe this is due to not having to do all the list construction and concatenation
# that ends up happening in operator_combinations
def valid(test, values, operators):
def _valid(current, values):
if len(values) == 0:
return current == test
for op in operators:
if _valid(op(current, values[0]), values[1:]):
return True
return False
return _valid(0, values)
def part_one_alt(data):
equations = [ints(line) for line in data.splitlines()]
v = 0
for test, *values in equations:
if valid(test, values, [operator.add, operator.mul]):
v += test
return v
def part_two_alt(data):
equations = [ints(line) for line in data.splitlines()]
v = 0
for test, *values in equations:
if valid(test, values, [operator.add, operator.mul, concat]):
v += test
return v
print(part_one_alt(data))
print(part_two_alt(data))