Skip to content

Commit 9828e8a

Browse files
author
tuukkasarvi
committed
Day 13
1 parent 7177376 commit 9828e8a

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

data/input_13.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1014511
2+
17,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,643,x,x,x,x,x,x,x,23,x,x,x,x,13,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,29,x,433,x,x,x,x,x,37,x,x,x,x,x,x,x,x,x,x,x,x,19

day_13.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
1.
3+
What is the ID of the earliest bus you can take to the airport multiplied by the number of
4+
minutes you'll need to wait for that bus?
5+
6+
2.
7+
What is the earliest timestamp such that all of the listed bus IDs depart at offsets matching
8+
their positions in the list?
9+
"""
10+
import numpy as np
11+
12+
filename = 'data/input_13.txt'
13+
14+
15+
def part1():
16+
with open(filename) as fp:
17+
lines = [line.strip("\n") for line in fp]
18+
departure_after = int(lines[0])
19+
buses = [int(item) for item in lines[1].split(",") if item != "x"]
20+
departures = [departure_after - (departure_after % bus) + bus for bus in buses]
21+
departure_earliest = min(departures)
22+
bus = buses[departures.index(departure_earliest)]
23+
print("Answer %d" % (bus * (departure_earliest - departure_after)))
24+
25+
26+
def is_solution(t, ids, offsets, l):
27+
""" conditions are of form (t + offset) % ID = 0"""
28+
return all([((t + offsets[i]) % ids[i]) == 0 for i in range(l)])
29+
30+
31+
def eda(ids, offsets):
32+
""" It so happens that third offset (17 time units) from t must be divisible by
33+
643, 17, 13, 29 and 37.
34+
"""
35+
print([643] + list(np.array(ids)[np.array(ids) == np.abs(np.array(offsets) - 17)]))
36+
37+
38+
def search(ids, offsets, l):
39+
""" Search over all multiples of 643 * 17 * 13 * 29 * 37 = 152476519.
40+
Third offset from t must be divisible by 643, 17, 13, 29 and 37.
41+
(t + 17) % 152476519 = 0.
42+
"""
43+
a = 152476519
44+
t = a - 17
45+
print("Max solution: %d" % np.prod(ids))
46+
print("Max iterations: %d" % (np.prod(ids) / a))
47+
print("Average iterations: %d" % (np.prod(ids)/(2*a)))
48+
while not is_solution(t, ids, offsets, l):
49+
t += a
50+
# print(t)
51+
print("Iterations required: %d" % ((t + 17) / a))
52+
return t
53+
54+
55+
def part2():
56+
with open(filename) as fp:
57+
lines = [line.strip("\n") for line in fp]
58+
data = ["x" if item == "x" else int(item) for item in lines[1].split(",")]
59+
ids = [int(item) for item in lines[1].split(",") if item != "x"]
60+
offsets = [data.index(id) for id in ids]
61+
l = len(ids)
62+
# eda(ids, offsets)
63+
t = search(ids, offsets, l)
64+
print("Answer: %d" % t)
65+
66+
67+
if __name__ == '__main__':
68+
part1()
69+
print("****")
70+
part2()

0 commit comments

Comments
 (0)