diff --git a/2021/common.nim b/2021/common.nim index f39e23a..510053a 100644 --- a/2021/common.nim +++ b/2021/common.nim @@ -20,6 +20,8 @@ proc inputStream*(day: int): Stream = proc toInts*(s: seq[string]): seq[int] = s.map(parseInt) +proc loadInputText*(day: int): string = day.inputFilePath().readFile().strip() + proc loadInput*(day: int): seq[string] = result = collect: for l in day.inputStream().lines(): l diff --git a/2021/readme.md b/2021/readme.md index 5aec99c..c06b29c 100644 --- a/2021/readme.md +++ b/2021/readme.md @@ -43,7 +43,7 @@ Or - [x] [Day 3](./3.ts) - [x] [Day 4](./four.nim) - [x] [Day 5](./five.nim) -- [ ] Day 6 +- [x] [Day 6](./six.nim) - [ ] Day 7 - [ ] Day 8 - [ ] Day 9 diff --git a/2021/six.nim b/2021/six.nim index 8be2059..689a143 100644 --- a/2021/six.nim +++ b/2021/six.nim @@ -5,7 +5,7 @@ const DEFAULT_DAYS = 80 const CYCLE_DAYS = 7 const MATURING_DAYS = 2 -proc lanternFish(inputLines: seq[string], days = DEFAULT_DAYS): int = +proc lanternFish(input: string, days = DEFAULT_DAYS): int = # a place to put newly-spawned fish that are not mature enough themselves to # spawn within the cycle var maturingFish: seq[(int, int)] @@ -14,7 +14,7 @@ proc lanternFish(inputLines: seq[string], days = DEFAULT_DAYS): int = var fish = newSeq[int](CYCLE_DAYS) # initialize our current fish counts based on the input - for i in inputLines[0].split(',').mapIt(it.parseInt()): inc fish[i] + for i in input.split(',').mapIt(it.parseInt()): inc fish[i] for day in 0..days: let cycleDay = day mod CYCLE_DAYS @@ -34,12 +34,12 @@ proc lanternFish(inputLines: seq[string], days = DEFAULT_DAYS): int = # sum all fish counts plus the fish that were spawned two days ago fish.foldl(a + b) + (if maturingFish[0][0] <= 1: maturingFish[0][1] else: 0) -let input = 6.loadInput() +let input = 6.loadInputText() time("day 6 part 1"): echo input.lanternFish() time("day 6 part 2"): echo input.lanternFish(256) when not defined(release): - let testInput = @["3,4,3,1,2"] + let testInput = "3,4,3,1,2" doAssert testInput.lanternFish(18) == 26 doAssert testInput.lanternFish() == 5934 doAssert testInput.lanternFish(256) == 26984457539