From 3983a072b0505d9ed0f1c4bb5314cd115e5ede89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Guti=C3=A9rrez=20de=20Quevedo=20P=C3=A9?= =?UTF-8?q?rez?= Date: Sun, 13 Dec 2020 22:06:40 +0100 Subject: [PATCH] add day 13 --- 13/1.py | 9 +++++++++ 13/2.py | 31 +++++++++++++++++++++++++++++++ 13/einput | 2 ++ 13/input | 2 ++ 4 files changed, 44 insertions(+) create mode 100644 13/1.py create mode 100644 13/2.py create mode 100644 13/einput create mode 100644 13/input diff --git a/13/1.py b/13/1.py new file mode 100644 index 0000000..1ec0350 --- /dev/null +++ b/13/1.py @@ -0,0 +1,9 @@ +with open("input") as f: + content = [x.strip() for x in f] + +timestamp = int(content[0]) +buses = [int(x) for x in content[1].split(",") if x != "x"] +next_deps = [((int(timestamp / x) + 1) * x) - timestamp for x in buses] +next_dep = min(next_deps) +next_bus = buses[next_deps.index(next_dep)] +print(next_bus * next_dep) diff --git a/13/2.py b/13/2.py new file mode 100644 index 0000000..1593b3f --- /dev/null +++ b/13/2.py @@ -0,0 +1,31 @@ +def is_correct_timestamp(buses, timestamp): + for pos, bus in buses: + if (timestamp + pos) % bus != 0: + return False + return True + +# t -> initial timestamp +# dx -> dx +def doa2(buses, t, dx): + timestamp = t + while True: + if is_correct_timestamp(buses, timestamp): + break + timestamp += dx + + return timestamp + +with open("input") as f: + content = [x.strip() for x in f] + +splcont = content[1].split(",") +tbuses = zip(range(len(splcont)), [int(x) if x != "x" else None for x in splcont]) +buses = list(filter(lambda x: x[1] != None, tbuses)) + +dx = buses[0][1] +t = buses[0][0] +for i in range(1, len(buses)): + t = doa2(buses[i:i+2], t, dx) + dx *= buses[i][1] + +print(t) diff --git a/13/einput b/13/einput new file mode 100644 index 0000000..e473080 --- /dev/null +++ b/13/einput @@ -0,0 +1,2 @@ +939 +7,13,x,x,59,x,31,19 \ No newline at end of file diff --git a/13/input b/13/input new file mode 100644 index 0000000..0a5136f --- /dev/null +++ b/13/input @@ -0,0 +1,2 @@ +1000507 +29,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,631,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,383,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,17