Skip to content

Commit cb4d51c

Browse files
authored
Merge pull request #92 from MaritimeOptima/day13/preng
Day 13 python by preng
2 parents 169c7d6 + d9ffe75 commit cb4d51c

File tree

4 files changed

+87
-4
lines changed

4 files changed

+87
-4
lines changed

days/day-13/io/preng.input

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

days/day-13/io/preng.output

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
4722
2+
825305207525452
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from functools import reduce
2+
from sys import stdin
3+
import math
4+
5+
def get_stdin():
6+
return [line.rstrip() for line in stdin]
7+
8+
lines = get_stdin()
9+
10+
# PART 1
11+
12+
earliest = int(lines[0])
13+
buses = [int(n) for n in lines[1].split(",") if n != "x"]
14+
15+
now = False
16+
t = earliest
17+
while not now:
18+
for bus in buses:
19+
if t % bus == 0:
20+
#print("success at", t, "with", bus)
21+
print((t-earliest) * bus)
22+
now = True
23+
t += 1
24+
25+
26+
# PART 2
27+
28+
def busno(num):
29+
if num == "x":
30+
return -1
31+
else:
32+
return int(num)
33+
34+
# Googled modulo equations and the Chinese theorem. Not fully understanding why this works.
35+
def mul_inv(a, b):
36+
b0 = b
37+
x0, x1 = 0, 1
38+
if b == 1: return 1
39+
while a > 1:
40+
q = a // b
41+
a, b = b, a%b
42+
x0, x1 = x1 - q * x0, x0
43+
if x1 < 0: x1 += b0
44+
return x1
45+
46+
def chinese_remainder(n, a):
47+
sum = 0
48+
prod = reduce(lambda a, b: a*b, n)
49+
for n_i, a_i in zip(n, a):
50+
p = prod // n_i
51+
sum += a_i * mul_inv(p, n_i) * p
52+
return sum % prod
53+
54+
def find_earliest(buses):
55+
modulos = [n for n in buses if n != -1]
56+
deltas = []
57+
for i in range(len(buses)):
58+
if buses[i] != -1:
59+
deltas.append(-i)
60+
#print(modulos, deltas)
61+
return(chinese_remainder(modulos, deltas))
62+
63+
tests = [
64+
[-1,-1,3,5],
65+
[7,13,-1,-1,59,-1,31,19],
66+
[17,-1,13,19],
67+
[67,7,59,61],
68+
[67,-1,7,59,61],
69+
[67,7,-1,59,61],
70+
[1789,37,47,1889]
71+
]
72+
73+
# for t in tests:
74+
# print(find_earliest(t), t)
75+
# print("")
76+
# exit(0)
77+
78+
buses = [busno(n) for n in lines[1].split(",")]
79+
80+
#print(buses, len(buses))
81+
print(find_earliest(buses))

days/day-13/test.sh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ set -euo pipefail
33

44
D=$(dirname $(realpath $0))
55

6-
echo "--- Day 13: --- ????? ---"
6+
echo "--- Day 13: --- Shuttle search ---"
77
#$D/../../lang/go.sh "$D/solutions/*.go" "$D/io/*"
88
#$D/../../lang/sml.sh "$D/solutions/*.sml" "$D/io/*"
9-
#$D/../../lang/python.sh "$D/solutions/*.py" "$D/io/*"
9+
$D/../../lang/python.sh "$D/solutions/*.py" "$D/io/*"
1010
#$D/../../lang/deno.sh "$D/solutions/*.ts" "$D/io/*"
11-
12-
exit 1337;

0 commit comments

Comments
 (0)