Day 13
This commit is contained in:
2
day13/input.txt
Normal file
2
day13/input.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
1007153
|
||||
29,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,37,x,x,x,x,x,433,x,x,x,x,x,x,x,x,x,x,x,x,13,17,x,x,x,x,19,x,x,x,23,x,x,x,x,x,x,x,977,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,41
|
||||
7
day13/part1.py
Normal file
7
day13/part1.py
Normal file
@@ -0,0 +1,7 @@
|
||||
lines = [x.strip() for x in open("input.txt")]
|
||||
|
||||
timestamp = int(lines[0])
|
||||
busses = (int(x) for x in lines[1].split(',') if x != 'x')
|
||||
next_departures = ((x, timestamp // x * x + x) for x in busses)
|
||||
next_bus = min(next_departures, key=lambda x: x[1])
|
||||
print(next_bus[0] * (next_bus[1]-timestamp))
|
||||
30
day13/part2.py
Normal file
30
day13/part2.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import math
|
||||
|
||||
def lcm(a, b):
|
||||
return abs(a*b) // math.gcd(a, b)
|
||||
|
||||
|
||||
# an+b=xn+y -> n=(y-b)/(a-x)
|
||||
# -> cn+d
|
||||
def merge(a, b, x, y):
|
||||
d1 = b
|
||||
d2 = y
|
||||
while d1 != d2:
|
||||
if d1 < d2:
|
||||
d1 += a * ((d2-d1-1) // a + 1)
|
||||
else:
|
||||
d2 += x * ((d1-d2-1) // x + 1)
|
||||
d = d1
|
||||
c = lcm(a, x)
|
||||
return c, d
|
||||
|
||||
|
||||
lines = [x.strip() for x in open("input.txt")]
|
||||
busses = ((int(x), -idx) for idx, x in enumerate(lines[1].split(',')) if x != 'x')
|
||||
|
||||
current = (1, 0)
|
||||
for bus in busses:
|
||||
current = merge(*current, *bus)
|
||||
|
||||
|
||||
print(current[1])
|
||||
Reference in New Issue
Block a user