diff options
author | j-james | 2021-01-13 00:18:06 +0000 |
---|---|---|
committer | j-james | 2021-01-13 02:08:42 +0000 |
commit | b4dcb59a8ace92d2a4343ad41a9230b727b1b850 (patch) | |
tree | a8af73116d45ed5381f94e3c67a745819b844405 | |
parent | d0cf198dd662a3907687b9bb5397f76504b7700a (diff) |
Day Fifteen
-rw-r--r-- | 2020/fifteen.nim | 20 | ||||
-rw-r--r-- | 2020/input/fifteen.txt | 1 |
2 files changed, 21 insertions, 0 deletions
diff --git a/2020/fifteen.nim b/2020/fifteen.nim new file mode 100644 index 0000000..0086054 --- /dev/null +++ b/2020/fifteen.nim @@ -0,0 +1,20 @@ +# Day Fifteen: Rambunctious Recitation +import os, strutils, sequtils + +let input: string = paramStr(1) + +proc recite(nth: int): int = + var numbers: seq[int] = map(split(strip(readFile(input)), ","), parseInt) + var history: seq[int] = newSeqWith[int](nth, -1) + for i in 0 .. nth - 2: + let previous: int = numbers[i] + if i == len(numbers) - 1: + if history[previous] != -1: + numbers.add(i - history[previous]) + else: + numbers.add(0) + history[previous] = i + return numbers[^1] + +echo recite(2020) +echo recite(30000000) diff --git a/2020/input/fifteen.txt b/2020/input/fifteen.txt new file mode 100644 index 0000000..ef94aa4 --- /dev/null +++ b/2020/input/fifteen.txt @@ -0,0 +1 @@ +20,0,1,11,6,3 |