-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_07.py
74 lines (60 loc) · 1.76 KB
/
day_07.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
import aoc_helper
raw = aoc_helper.fetch(7, 2024)
def parse_raw(raw: str):
a = []
for i in raw.splitlines():
x,y = i.split(':',maxsplit=1)
a.append((int(x), [int(k) for k in y.split()]))
return a
data = parse_raw(raw)
def evalu(a, operations):
b = a[::-1]
for i in operations:
x,y = b.pop(), b.pop()
if i == 1: b.append(x*y)
else: b.append(x+y)
# print(a, operations, b)
return b[0]
def evalu2(a, operations):
b = a[::-1]
for i in operations:
x,y = b.pop(), b.pop()
if i == 1: b.append(x*y)
elif i == 2: b.append(x+y)
else: b.append(int(f'{x}{y}'))
# print(a, operations, b)
return b[0]
def part_one(data=data):
ans = 0
for target, hmm in data:
n = len(hmm)
for mask in range(1<<(n-1)):
ops = []
temp = mask
for j in range(n-1):
ops.append(temp & 1)
temp >>= 1
ev = evalu(hmm, ops)
if ev == target:
ans += target
break
return ans
aoc_helper.lazy_test(day=7, year=2024, parse=parse_raw, solution=part_one)
def part_two(data=data):
ans = 0
for target, hmm in data:
n = len(hmm)
for mask in range(3**(n-1)):
ops = []
temp = mask
for j in range(n-1):
ops.append(temp % 3)
temp //= 3
ev = evalu2(hmm, ops)
if ev == target:
ans += target
break
return ans
aoc_helper.lazy_test(day=7, year=2024, parse=parse_raw, solution=part_two)
aoc_helper.lazy_submit(day=7, year=2024, solution=part_one, data=data)
aoc_helper.lazy_submit(day=7, year=2024, solution=part_two, data=data)