aoc2020/13/2.py

32 lines
758 B
Python
Raw Permalink Normal View History

2020-12-13 22:06:40 +01:00
def is_correct_timestamp(buses, timestamp):
for pos, bus in buses:
if (timestamp + pos) % bus != 0:
return False
return True
# t -> initial timestamp
# dx -> dx
2020-12-13 22:08:57 +01:00
def get_next_timestamp(buses, t, dx):
2020-12-13 22:06:40 +01:00
timestamp = t
while True:
if is_correct_timestamp(buses, timestamp):
break
timestamp += dx
return timestamp
with open("input") as f:
content = [x.strip() for x in f]
splcont = content[1].split(",")
tbuses = zip(range(len(splcont)), [int(x) if x != "x" else None for x in splcont])
buses = list(filter(lambda x: x[1] != None, tbuses))
dx = buses[0][1]
t = buses[0][0]
for i in range(1, len(buses)):
2020-12-13 22:08:57 +01:00
t = get_next_timestamp(buses[i:i+2], t, dx)
2020-12-13 22:06:40 +01:00
dx *= buses[i][1]
print(t)