From 0dcf0cb22cd4ac210677cae7ea16df88c2b878bb Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 13 Dec 2020 09:41:21 +0100 Subject: [PATCH] Solve day 13 part 1 and nearly 2 --- 2020/day13_bus.py | 69 +++++++++++++++++++++++++++++++++++++++++ 2020/inputs/day13 | 2 ++ 2020/inputs/day13-test1 | 2 ++ 2020/inputs/day13-test2 | 2 ++ 2020/inputs/day13-test3 | 2 ++ 2020/inputs/day13-test4 | 2 ++ 2020/inputs/day13-test5 | 2 ++ 2020/inputs/day13-test6 | 2 ++ 8 files changed, 83 insertions(+) create mode 100644 2020/day13_bus.py create mode 100644 2020/inputs/day13 create mode 100644 2020/inputs/day13-test1 create mode 100644 2020/inputs/day13-test2 create mode 100644 2020/inputs/day13-test3 create mode 100644 2020/inputs/day13-test4 create mode 100644 2020/inputs/day13-test5 create mode 100644 2020/inputs/day13-test6 diff --git a/2020/day13_bus.py b/2020/day13_bus.py new file mode 100644 index 0000000..d1a4877 --- /dev/null +++ b/2020/day13_bus.py @@ -0,0 +1,69 @@ +import enum +import functools +import math +from typing import List, Dict + + +def main(filename: str, expected_part_1: int = None, expected_part_2: int = None): + print(f"\n+ Running on {filename}") + with open(filename) as f: + notes = f.read().strip().split("\n") + + counter_part_1 = solve_part_1(notes) + + print(f"1. Found {counter_part_1}") + if expected_part_1: + assert expected_part_1 == counter_part_1 + + counter_part_2 = solve_part_2(notes) + print(f"2. Found {counter_part_2}") + if expected_part_2: + assert expected_part_2 == counter_part_2 + + +def solve_part_1(notes) -> int: + earliest = int(notes[0]) + buses = map(int, filter(lambda bus: bus != "x", notes[1].split(","))) + min_delta = math.inf + min_bus = None + for bus in buses: + delta = math.ceil(earliest / bus) * bus - earliest + if delta < min_delta: + min_delta = delta + min_bus = bus + + return min_bus * min_delta + + +def solve_part_2(notes) -> int: + """Works but far too slow for the real deal.""" + split = notes[1].split(",") + buses = list(map(int, filter(lambda bus: bus != "x", split))) + minutes = {} # type: Dict[int, int] + biggest_bus = max(buses) + biggest_bus_index = split.index(str(biggest_bus)) + + for bus in buses: + minutes[split.index(str(bus)) - biggest_bus_index] = bus + + found = False + timestamp = 99999999999628 + while not found: + timestamp += biggest_bus + found = True + for delta, bus in minutes.items(): + if (timestamp + delta) % bus != 0: + found = False + break + + return timestamp + min(minutes.keys()) + + +if __name__ == "__main__": + main("inputs/day13-test1", 295, 1068781) + main("inputs/day13-test2", None, 3417) + main("inputs/day13-test3", None, 754018) + main("inputs/day13-test4", None, 779210) + main("inputs/day13-test5", None, 1261476) + main("inputs/day13-test6", None, 1202161486) + main("inputs/day13", 4315) diff --git a/2020/inputs/day13 b/2020/inputs/day13 new file mode 100644 index 0000000..6d4ea89 --- /dev/null +++ b/2020/inputs/day13 @@ -0,0 +1,2 @@ +1001938 +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,431,x,x,x,x,x,x,x,23,x,x,x,x,13,x,x,x,17,x,19,x,x,x,x,x,x,x,x,x,x,x,863,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,29 diff --git a/2020/inputs/day13-test1 b/2020/inputs/day13-test1 new file mode 100644 index 0000000..e473080 --- /dev/null +++ b/2020/inputs/day13-test1 @@ -0,0 +1,2 @@ +939 +7,13,x,x,59,x,31,19 \ No newline at end of file diff --git a/2020/inputs/day13-test2 b/2020/inputs/day13-test2 new file mode 100644 index 0000000..7bcbba0 --- /dev/null +++ b/2020/inputs/day13-test2 @@ -0,0 +1,2 @@ +939 +17,x,13,19 \ No newline at end of file diff --git a/2020/inputs/day13-test3 b/2020/inputs/day13-test3 new file mode 100644 index 0000000..eb10fd9 --- /dev/null +++ b/2020/inputs/day13-test3 @@ -0,0 +1,2 @@ +939 +67,7,59,61 \ No newline at end of file diff --git a/2020/inputs/day13-test4 b/2020/inputs/day13-test4 new file mode 100644 index 0000000..9e3ef37 --- /dev/null +++ b/2020/inputs/day13-test4 @@ -0,0 +1,2 @@ +939 +67,x,7,59,61 \ No newline at end of file diff --git a/2020/inputs/day13-test5 b/2020/inputs/day13-test5 new file mode 100644 index 0000000..355d02c --- /dev/null +++ b/2020/inputs/day13-test5 @@ -0,0 +1,2 @@ +939 +67,7,x,59,61 \ No newline at end of file diff --git a/2020/inputs/day13-test6 b/2020/inputs/day13-test6 new file mode 100644 index 0000000..fd0a4d0 --- /dev/null +++ b/2020/inputs/day13-test6 @@ -0,0 +1,2 @@ +939 +1789,37,47,1889 \ No newline at end of file