-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday13.py
executable file
·66 lines (50 loc) · 1.1 KB
/
day13.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
#!/usr/bin/env python3
from utils.all import *
advent.setup(2020, 13)
fin = advent.get_input()
# fin = io.StringIO('''\
# 939
# 7,13,x,x,59,x,31,19
# ''')
# eprint(*fin, sep=''); fin.seek(0, 0)
timer_start()
target = int(fin.readline())
raw = fin.readline().strip().split(',')
times = filter(lambda x: x != 'x', raw)
times = list(map(int, times))
best = 99999999999999999
ans = -1
for t in times:
if target % t == 0:
mul = target // t
else:
mul = target // t + 1
delta = mul * t - target
# eprint(t, best, delta)
if delta < best:
best = delta
ans = t * delta
advent.print_answer(1, ans)
# Google search "python chinese remainder theorem"
from functools import reduce
def chinese_remainder(n, a):
ssum = 0
prod = reduce(lambda a, b: a*b, n)
for n_i, a_i in zip(n, a):
p = prod // n_i
ssum += a_i * pow(p, -1, n_i) * p
return ssum % prod
buses = []
for i, t in enumerate(raw):
if t != 'x':
buses.append((i, int(t)))
_n = []
_a = []
for a, n in buses:
a = a % n
a = (n-a) % n
# eprint('t ===', a, 'mod', n)
_n.append(n)
_a.append(a)
t = chinese_remainder(_n, _a)
advent.print_answer(2, t)