This commit is contained in:
parent
b76bb1c284
commit
1139eb9cd6
|
@ -0,0 +1,12 @@
|
|||
# 13
|
||||
|
||||
The second part one was really interesting. While my solution is correct, it takes way to much time in the data set.
|
||||
I started by checking what is the biggest ID number and check each location.
|
||||
|
||||
<details>
|
||||
<summary>Solutions</summary>
|
||||
<ol>
|
||||
<li>156</li>
|
||||
<li>404517869995362</li>
|
||||
</ol>
|
||||
</details>
|
|
@ -0,0 +1,2 @@
|
|||
1000299
|
||||
41,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,37,x,x,x,x,x,971,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,17,13,x,x,x,x,23,x,x,x,x,x,29,x,487,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,19
|
|
@ -0,0 +1,76 @@
|
|||
from os.path import join, dirname
|
||||
from typing import List, Tuple
|
||||
from math import ceil, floor
|
||||
|
||||
|
||||
class Station:
|
||||
|
||||
def __init__(self, txt: str) -> None:
|
||||
arrival, busses = txt.strip().split('\n')
|
||||
self.arrival = int(arrival)
|
||||
self.busses = [
|
||||
int(bus)
|
||||
for bus in busses.replace('x', '0').split(',')
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def get_next_for_id(id: int, arrival: int) -> int:
|
||||
return id * ceil(arrival / id)
|
||||
|
||||
def find_next(self) -> Tuple[int, int]:
|
||||
arrivals: List[Tuple[int, int]] = []
|
||||
for bus in self.busses:
|
||||
if bus == 0:
|
||||
continue
|
||||
arrivals.append((bus, self.get_next_for_id(bus, self.arrival)))
|
||||
return min(arrivals, key=lambda x: x[1])
|
||||
|
||||
def get_flag(self) -> int:
|
||||
id, arrives = self.find_next()
|
||||
return id * (arrives - self.arrival)
|
||||
|
||||
def contest(self, offset: int = 1) -> int:
|
||||
# Prepare
|
||||
highest = max(self.busses)
|
||||
highest_i = self.busses.index(highest)
|
||||
others = [
|
||||
(bus, i - highest_i)
|
||||
for i, bus in enumerate(self.busses)
|
||||
if bus != 0 and i != highest_i
|
||||
]
|
||||
others.sort(key=lambda x: x[0], reverse=True)
|
||||
|
||||
# Compute
|
||||
i: int = max(1, floor(offset / highest))
|
||||
while True:
|
||||
x = highest * i
|
||||
error = False
|
||||
for bus, diff in others:
|
||||
dx = x + diff
|
||||
if dx != self.get_next_for_id(bus, dx):
|
||||
error = True
|
||||
break
|
||||
if not error:
|
||||
return x - highest_i
|
||||
i += 1
|
||||
|
||||
|
||||
data = join(dirname(__file__), '../data.txt')
|
||||
with open(data) as f:
|
||||
|
||||
# Some "testing"
|
||||
all = {
|
||||
'17,x,13,19': 3417,
|
||||
'67,7,59,61': 754018,
|
||||
'67,x,7,59,61': 779210,
|
||||
'67,7,x,59,61': 1261476,
|
||||
'1789,37,47,1889': 1202161486,
|
||||
}
|
||||
for busses, expected in all.items():
|
||||
station = Station('69\n' + busses)
|
||||
print(expected, expected == station.contest())
|
||||
|
||||
txt = f.read()
|
||||
station = Station(txt)
|
||||
print(station.get_flag())
|
||||
print(station.contest(offset=10**14))
|
|
@ -0,0 +1,2 @@
|
|||
939
|
||||
7,13,x,x,59,x,31,19
|
|
@ -0,0 +1,2 @@
|
|||
69
|
||||
67,7,x,59,61
|
Loading…
Reference in New Issue