This commit is contained in:
a 2020-12-13 01:13:19 -06:00
parent 7f94fcfba0
commit 25b35e8f5c
3 changed files with 59 additions and 0 deletions

55
2020/13/13.py Normal file
View File

@ -0,0 +1,55 @@
def read_file():
with open("input.txt","r") as f:
timestamp, buses = f.read().split('\n')
timestamp = int(timestamp)
buses = buses.split(',')
working_buses = [int(b) for b in buses if b != "x"]
return timestamp, working_buses, buses
def part1(timestamp, buses):
waits = [abs((timestamp % b) - b) for b in buses]
i = waits.index(min(waits))
return buses[i] * (waits[i])
from functools import reduce
def crt(a,n):
sum=0
product =reduce(lambda a, b: a*b, n)
for n_i, a_i in zip(n,a):
p = product // n_i
sum += a_i * mul_inv(p, n_i) * p
return sum % product
def mul_inv(a, b):
b0 = b
x0, x1 = 0, 1
if b == 1:
return 1
while a > 1:
q = a // b
a, b = b, (a % b)
x0, x1 = (x1 - q * x0), x0
if x1 < 0:
x1 += b0
return x1
def part2(buses):
r = []
m = []
for i, b in enumerate(buses): # i = wait, b = modulo
if b == "x":
continue
b = int(b)
r.append(b-i) # convert to remainder
m.append(b)
return crt(r,m)
def main():
timestamp, working_buses, all_buses = read_file()
print(part1(timestamp, working_buses), part2(all_buses))
print(part2([17,"x",13,19])) # 3417
print(part2([67,7,59,61])) # 754018
print(part2([67,"x",7,59,61])) # 779210
print(part2([67,7,"x",59,61])) # 1261476
print(part2([1789,37,47,1889])) # 1202161486
if __name__ == "__main__":
main()

2
2020/13/input.txt Normal file
View File

@ -0,0 +1,2 @@
1000417
23,x,x,x,x,x,x,x,x,x,x,x,x,41,x,x,x,37,x,x,x,x,x,479,x,x,x,x,x,x,x,x,x,x,x,x,13,x,x,x,17,x,x,x,x,x,x,x,x,x,x,x,29,x,373,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,19

2
2020/13/input_sample.txt Normal file
View File

@ -0,0 +1,2 @@
939
7,13,x,x,59,x,31,19