Skip to content

Commit 9952d46

Browse files
author
Chris Poch
committed
Day 13
1 parent e0c3dbc commit 9952d46

File tree

8 files changed

+82
-0
lines changed

8 files changed

+82
-0
lines changed

13-1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
939
2+
7,13,x,x,59,x,31,19

13-2.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2
2+
17,x,13,19

13-3.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
3
2+
67,7,59,61

13-4.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
4
2+
67,x,7,59,61

13-5.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
5
2+
67,7,x,59,61

13-6.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
6
2+
1789,37,47,1889

13.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import os
2+
import sys
3+
4+
debug = False
5+
fileName = ""
6+
try:
7+
fileName = sys.argv[1]
8+
if len(fileName) < 1:
9+
fileName = "13.txt"
10+
except:
11+
fileName = "13.txt"
12+
print(fileName)
13+
14+
with open(fileName) as file:
15+
time = int(file.readline())
16+
note = file.readline()
17+
note2 = note.replace(",x","")
18+
buses = note2.split(",")
19+
for i in range(len(buses)):
20+
buses[i] = int(buses[i])
21+
leaves = []
22+
23+
for i in range(len(buses)):
24+
leaves.append(time + buses[i] - (time % buses[i]))
25+
26+
min = leaves[0]
27+
mini = 0
28+
for i in range(len(leaves)):
29+
if leaves[i] < min:
30+
min = leaves[i]
31+
mini = i
32+
wait = leaves[mini] - time
33+
print(buses[mini], wait, buses[mini] * wait)
34+
35+
#part 2
36+
print("Part 2")
37+
buses = note.split(",")
38+
#print(buses)
39+
for i in range(len(buses)):
40+
if buses[i] == "x":
41+
buses[i] = 0
42+
else:
43+
#print(buses[i])
44+
buses[i] = int(buses[i])
45+
leaves = []
46+
for bus in buses:
47+
leaves.append(0)
48+
49+
time = 0
50+
step = 1
51+
#influenced by https://gist.github.com/joshbduncan/65f810fe821c7a3ea81a1f5a444ea81e
52+
"""Since you must find departure times for each bus based off of the previous bus schedule (which is based on the previous bus and so on...), all following buses must depart at a multiple of all previous buses departures since they have to stay in the order provided.
53+
54+
Step Progression
55+
1 * 7 = 7
56+
7 * 13 = 91
57+
91 * 59 = 5369
58+
5369 * 31 = 166439
59+
60+
Then you just need to add the cumulative time (t) and the minute offset for each bus in the list to get the final answer."""
61+
p2 = [(int(i), j) for j, i in enumerate(buses) if i != 0]
62+
for bus, offset in p2:
63+
while (time + offset) % bus != 0:
64+
time += step
65+
step *= bus
66+
67+
print(time)
68+

13.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1000655
2+
17,x,x,x,x,x,x,x,x,x,x,37,x,x,x,x,x,571,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,13,x,x,x,x,23,x,x,x,x,x,29,x,401,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,19

0 commit comments

Comments
 (0)