aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorj-james2020-12-14 10:07:04 +0000
committerj-james2020-12-14 10:07:04 +0000
commit3732581182aa4ebca7bb0560f0ca378e009aa652 (patch)
tree3a6d86b216cba7810069c4fd7be0aa971d336686
parent07fb91a80974183657902213fd7a70c958eaf447 (diff)
Day Thirteen
-rw-r--r--2020/input/thirteen.txt2
-rw-r--r--2020/thirteen.nim29
2 files changed, 31 insertions, 0 deletions
diff --git a/2020/input/thirteen.txt b/2020/input/thirteen.txt
new file mode 100644
index 0000000..ca9d2bb
--- /dev/null
+++ b/2020/input/thirteen.txt
@@ -0,0 +1,2 @@
+1015292
+19,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,743,x,x,x,x,x,x,x,x,x,x,x,x,13,17,x,x,x,x,x,x,x,x,x,x,x,x,x,x,29,x,643,x,x,x,x,x,37,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,23
diff --git a/2020/thirteen.nim b/2020/thirteen.nim
new file mode 100644
index 0000000..d01e681
--- /dev/null
+++ b/2020/thirteen.nim
@@ -0,0 +1,29 @@
+# Day Thirteen: Shuttle Search
+import os, strutils, sequtils
+
+let input: string = paramStr(1)
+let arrival: int = parseInt(splitLines(readFile(input))[0])
+let buses: seq[int] = map(split(splitLines(readFile(input))[1], ','),
+ func (bus: string): int =
+ if bus == "x":
+ 0
+ else:
+ parseInt(bus))
+
+var earliest: tuple[id, wait: int] = (999, 999)
+for bus in buses:
+ if bus > 0:
+ if bus - (arrival mod bus) < earliest.wait:
+ earliest = (bus, bus - (arrival mod bus))
+echo earliest.id * earliest.wait
+
+# blatantly stolen code - what on earth is a chinese remainder theorem?
+# (c: lizthegrey)
+var timestamp: int = 0
+var step: int = 1
+for i, bus in buses:
+ if bus != 0:
+ while (timestamp+i) mod bus != 0:
+ timestamp += step
+ step *= bus
+echo timestamp