-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_13.py
120 lines (96 loc) · 3.03 KB
/
day_13.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from collections import Counter, defaultdict, deque
import aoc_helper
from aoc_helper import (
Grid,
PrioQueue,
SparseGrid,
decode_text,
extract_ints,
extract_iranges,
extract_ranges,
extract_uints,
frange,
irange,
iter,
list,
map,
multirange,
range,
search,
tail_call,
)
raw = aoc_helper.fetch(13, 2024)
def parse_raw(raw: str):
mach = raw.split("\n\n")
hmm = []
for m in mach:
cur = []
for x in m.splitlines():
cur.append(extract_ints(x))
hmm.append(cur)
return hmm
data = parse_raw(raw)
# providing this default is somewhat of a hack - there isn't any other way to
# force type inference to happen, AFAIK - but this won't work with standard
# collections (list, set, dict, tuple)
def part_one(data=data):
tok = 0
cnt = 0
for a,b,p in data:
# dbrute = None
# for x in range(101):
# for y in range(101):
# xx = x * a[0] + y * b[0]
# yy = x * a[1] + y * b[1]
# if xx == p[0] and yy == p[1]:
# k = 3 * x + y
# if dbrute is not None: dbrute = min(k, dbrute)
# else: dbrute = k
d = a[0]*b[1] - a[1]*b[0]
if d != 0:
lm = (p[0] * b[1] - p[1] * b[0]) # divide this by d
um = (-p[0] * a[1] + p[1] * a[0]) # divide this by d
if (lm % d or um %d ):
# if dbrute is not None: print(a,b,p,dbrute, lm, um)
continue
lm //= d
um //= d
if not (0 <= lm <= 100 and 0 <= um <= 100):
# if dbrute is not None: print(a,b,p,dbrute, lm, um)
continue
# print(lm, um, (a,b,p))
# if dbrute is None:
# print(a,b,p,lm,um)
tok += 3 * lm + um
cnt += 1
continue
# d is zero
assert False
return tok
aoc_helper.lazy_test(day=13, year=2024, parse=parse_raw, solution=part_one)
# providing this default is somewhat of a hack - there isn't any other way to
# force type inference to happen, AFAIK - but this won't work with standard
# collections (list, set, dict, tuple)
def part_two(data=data):
tok = 0
cnt = 0
for a,b,p in data:
d = a[0]*b[1] - a[1]*b[0]
if d != 0:
p[0] += 10000000000000
p[1] += 10000000000000
lm = (p[0] * b[1] - p[1] * b[0]) # divide this by d
um = (-p[0] * a[1] + p[1] * a[0]) # divide this by d
if (lm % d or um %d ): continue
lm //= d
um //= d
if lm < 0 or um < 0: continue
tok += 3 * lm + um
cnt += 1
continue
# d is zero
assert False
return tok
aoc_helper.lazy_test(day=13, year=2024, parse=parse_raw, solution=part_two)
aoc_helper.lazy_submit(day=13, year=2024, solution=part_one, data=data)
aoc_helper.lazy_submit(day=13, year=2024, solution=part_two, data=data)