## DEV Community is a community of 607,586 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

loading...

# Advent of Code 2020: Python Solution Day 13

This is a day 13 and again I forgot about the challenge but at last, I did it. First part was easy but for the second part I took a help from Reddit comment here.
I have uploaded all my 2020 solution on this repository.

Please share your solution too.

## Part 1

``````with open("day13.txt", "r") as fp:
lines = fp.readlines()
timestamp = int(lines[:-1])
bus_ids = [int(x) for x in lines.split(",") if x.isdigit()]

import numpy as np
timestamps = range(timestamp-50, timestamp+50)
valid = np.inf
diff = np.inf
bus_id = np.inf

for time in timestamps:
for bus in bus_ids:
if time%bus==0:
d = abs(time-timestamp)
if time>timestamp and d < diff:
valid = time
diff = d
bus_id = bus

bus_id*(valid-timestamp)

``````

## Second part

``````LINES=lines
start = int(LINES)
busses = ["x" if x == "x" else int(x) for x in LINES.split(",")]

def part2():
mods = {bus: -i % bus for i, bus in enumerate(busses) if bus != "x"}
print(mods)
vals = list(reversed(sorted(mods)))
val = mods[vals]
r = vals
for b in vals[1:]:
while val % b != mods[b]:
val += r
r *= b
return val
part2()
``````

## Discussion (0) 