2020 day 13 (not cute)

This commit is contained in:
Luke Hubmayer-Werner 2020-12-14 14:58:09 +10:30
parent d9318b5f62
commit b80ce4579a
1 changed files with 11 additions and 0 deletions

11
2020/day13.py Normal file
View File

@ -0,0 +1,11 @@
input_t = 1002392 # Short input so we'll just hardcode
input_busIDs = "23,x,x,x,x,x,x,x,x,x,x,x,x,41,x,x,x,37,x,x,x,x,x,421,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,17,x,19,x,x,x,x,x,x,x,x,x,29,x,487,x,x,x,x,x,x,x,x,x,x,x,x,13"
ids = {int(x) for x in input_busIDs.split(',') if x != 'x'}
next_arrivals = {x: x*(-(-input_t//x)) for x in ids} # -(-x//y) is ceil(x/y)
id, t = min(next_arrivals.items(), key=lambda x: x[1])
print(f'Part 1: Next bus is Bus {id} at t={t} -> {id*(t-input_t)}')
from sympy.ntheory.modular import crt
pairs = [(i,int(v)) for i,v in enumerate(input_busIDs.split(',')) if v != 'x']
print(f'Part 2: t = {crt([id for time,id in pairs], [-time for time,id in pairs])[0]}')