Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 93b5609

Browse filesBrowse files
committed
day 13
1 parent 595293f commit 93b5609
Copy full SHA for 93b5609

File tree

2 files changed

+21
-1
lines changed
Filter options

2 files changed

+21
-1
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
|[10](https://adventofcode.com/2020/day/10)|Adapter Array|[py](/day10/main.py), [alt](/day10/alt.py)|
1515
|[11](https://adventofcode.com/2020/day/11)|Seating System|[py](/day11/main.py)|
1616
|[12](https://adventofcode.com/2020/day/12)|Rain Risk|[py](/day12/main.py)|
17-
|[13](https://adventofcode.com/2020/day/13)|-|-|
17+
|[13](https://adventofcode.com/2020/day/13)|Shuttle Search|[py](/day13/main.py)|
1818
|[14](https://adventofcode.com/2020/day/14)|-|-|
1919
|[15](https://adventofcode.com/2020/day/15)|-|-|
2020
|[16](https://adventofcode.com/2020/day/16)|-|-|

‎day13/main.py

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import numpy as np
2+
from math import prod
3+
4+
with open("input.txt") as f:
5+
lines = [x.strip() for x in f]
6+
7+
arrival = int(lines[0])
8+
buses = [(i, int(e)) for i, e in enumerate(lines[1].split(",")) if e.isdigit()]
9+
10+
offsets, times = zip(*buses)
11+
b = [e - (arrival % e) for e in times]
12+
print(np.min(b) * times[np.argmin(b)])
13+
14+
def chinese_remainder(ns, rems):
15+
p = prod(ns)
16+
x = sum(r * (p // n) * pow(p // n, -1, n) for r, n in zip(rems, ns))
17+
return x % p
18+
19+
rems = [time-offset for offset, time in buses]
20+
print(chinese_remainder(times, rems))

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.