-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.2.py
54 lines (47 loc) · 1.31 KB
/
13.2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
with open('input/13.txt') as f:
notes = [x.strip() for x in f.readlines()]
time = int(notes[0])
buses = [int(x.replace('x', '0')) for x in notes[1].split(',')]
def get_bus_next_journey(bus, t):
n_trips = int(t/ bus) + 1;
next_trip = bus * n_trips
return next_trip - t
def is_valid(bus, t):
if not bus:
return True
return t % bus == 0
def productise(buses):
product = 1
for b in buses:
if not b:
continue
print(b, product)
product *= (b - buses.index(b))
return product
print('Part Two:', productise(buses))
# Part Two: 54038417347200
# didn't work :) worth a shot as a gut instinct
# Real solution
# Part Two: 471793476184394
# start with first bus and keep track of product of buses
t = 0
p = 1
# we want the first timestamp where all buses leave in subsequent minutes
# this is equivalent to all buses leaving in the same minute if their offset
# is removed
# a bus leaves if its modulus is 0
# t % id == 0
# and a bus at index x
# t + x % id == 0
for index, bus in enumerate(buses):
# ignore every minute buses
if not bus:
continue
# find the next time where this bus is valid
# step in sizes of last valid product
# (current bus and all previous buses are valid)
while (t + index) % bus != 0:
t += p
p *= bus
print('Part Two:', t)
# Part Two: 471793476184394