diff --git a/solutions/13/README.md b/solutions/13/README.md new file mode 100644 index 0000000..3dd8569 --- /dev/null +++ b/solutions/13/README.md @@ -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. + +
+ Solutions +
    +
  1. 156
  2. +
  3. 404517869995362
  4. +
+
diff --git a/solutions/13/data.txt b/solutions/13/data.txt new file mode 100644 index 0000000..11e789e --- /dev/null +++ b/solutions/13/data.txt @@ -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 diff --git a/solutions/13/python/main.py b/solutions/13/python/main.py new file mode 100644 index 0000000..bb596dc --- /dev/null +++ b/solutions/13/python/main.py @@ -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)) diff --git a/solutions/13/test.txt b/solutions/13/test.txt new file mode 100644 index 0000000..e473080 --- /dev/null +++ b/solutions/13/test.txt @@ -0,0 +1,2 @@ +939 +7,13,x,x,59,x,31,19 \ No newline at end of file diff --git a/solutions/13/train.txt b/solutions/13/train.txt new file mode 100644 index 0000000..7cc453d --- /dev/null +++ b/solutions/13/train.txt @@ -0,0 +1,2 @@ +69 +67,7,x,59,61 \ No newline at end of file