-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday13.py
executable file
·42 lines (29 loc) · 860 Bytes
/
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
#!/usr/bin/env python3
import sys
from math import inf, gcd
from itertools import count
def lcm(a, b):
return a * b // gcd(a, b)
# Open the first argument as input or use stdin if no arguments were given
fin = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin
arrival = int(fin.readline())
raw = fin.readline().strip().split(',')
buses = []
for i, v in filter(lambda iv: iv[1] != 'x', enumerate(raw)):
buses.append((i, int(v)))
best = inf
for _, period in buses:
n = arrival // period + (arrival % period != 0)
wait = n * period - arrival
if wait < best:
best = wait
best_p = period
ans = best_p * best
print('Part 1:', ans)
time, step = buses[0]
for delta, period in buses[1:]:
for time in count(time, step):
if (time + delta) % period == 0:
break
step = lcm(step, period) # math.lcm() on Python >= 3.9
print('Part 2:', time)