Solve 2020 day 13 CRT jojo
This commit is contained in:
39
2020/d13.py
Normal file
39
2020/d13.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from lib import get_data, str_to_ints, mod_inverse
|
||||
from functools import reduce
|
||||
|
||||
data = get_data(__file__)
|
||||
lines = data.splitlines()
|
||||
|
||||
earliest = int(lines[0])
|
||||
times = str_to_ints(lines[1])
|
||||
|
||||
mintime = 10**21
|
||||
id = None
|
||||
|
||||
for time in times:
|
||||
mintimecur = (earliest // time) * time + time
|
||||
if mintimecur < mintime:
|
||||
mintime = mintimecur
|
||||
id = time
|
||||
|
||||
|
||||
assert id is not None
|
||||
print((mintime - earliest) * id)
|
||||
|
||||
fields = lines[1].split(",")
|
||||
|
||||
buses = []
|
||||
for offset, busid in enumerate(fields):
|
||||
if busid == "x":
|
||||
continue
|
||||
period = int(busid)
|
||||
buses.append((period, -offset % period))
|
||||
|
||||
|
||||
total = 0
|
||||
product = reduce(lambda a, b: a * b, map(lambda x: x[0], buses))
|
||||
for period, offset in buses:
|
||||
p = product // period
|
||||
total += offset * mod_inverse(p, period) * p
|
||||
|
||||
print(total % product)
|
||||
Reference in New Issue
Block a user