This commit is contained in:
Daniel Flanagan 2021-12-06 09:51:20 -06:00
parent 6761e4725d
commit 13042e1de2
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
3 changed files with 7 additions and 5 deletions

View file

@ -20,6 +20,8 @@ proc inputStream*(day: int): Stream =
proc toInts*(s: seq[string]): seq[int] = s.map(parseInt) 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] = proc loadInput*(day: int): seq[string] =
result = collect: result = collect:
for l in day.inputStream().lines(): l for l in day.inputStream().lines(): l

View file

@ -43,7 +43,7 @@ Or
- [x] [Day 3](./3.ts) - [x] [Day 3](./3.ts)
- [x] [Day 4](./four.nim) - [x] [Day 4](./four.nim)
- [x] [Day 5](./five.nim) - [x] [Day 5](./five.nim)
- [ ] Day 6 - [x] [Day 6](./six.nim)
- [ ] Day 7 - [ ] Day 7
- [ ] Day 8 - [ ] Day 8
- [ ] Day 9 - [ ] Day 9

View file

@ -5,7 +5,7 @@ const DEFAULT_DAYS = 80
const CYCLE_DAYS = 7 const CYCLE_DAYS = 7
const MATURING_DAYS = 2 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 # a place to put newly-spawned fish that are not mature enough themselves to
# spawn within the cycle # spawn within the cycle
var maturingFish: seq[(int, int)] var maturingFish: seq[(int, int)]
@ -14,7 +14,7 @@ proc lanternFish(inputLines: seq[string], days = DEFAULT_DAYS): int =
var fish = newSeq[int](CYCLE_DAYS) var fish = newSeq[int](CYCLE_DAYS)
# initialize our current fish counts based on the input # 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: for day in 0..days:
let cycleDay = day mod CYCLE_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 # 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) 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 1"): echo input.lanternFish()
time("day 6 part 2"): echo input.lanternFish(256) time("day 6 part 2"): echo input.lanternFish(256)
when not defined(release): 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(18) == 26
doAssert testInput.lanternFish() == 5934 doAssert testInput.lanternFish() == 5934
doAssert testInput.lanternFish(256) == 26984457539 doAssert testInput.lanternFish(256) == 26984457539