Add improved day 6 code with comments
This commit is contained in:
parent
bceae5f51f
commit
5de6ebf919
1 changed files with 39 additions and 0 deletions
39
day6/p2improved.nim
Normal file
39
day6/p2improved.nim
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
import os, sequtils
|
||||||
|
import std/strutils
|
||||||
|
|
||||||
|
const SIM_DAYS = 256
|
||||||
|
const FISH_DAYS = 8
|
||||||
|
|
||||||
|
proc main() =
|
||||||
|
let fileName = paramStr(1)
|
||||||
|
|
||||||
|
let f = fileName.open(fmRead)
|
||||||
|
|
||||||
|
var lanternFishSim: array[0..FISH_DAYS, int]
|
||||||
|
|
||||||
|
# get all of the initial counts of fish
|
||||||
|
for fishDay in f.readLine().split(",").mapIt(it.parseInt):
|
||||||
|
lanternFishSim[fishDay] += 1
|
||||||
|
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
|
for i in 0 ..< SIM_DAYS:
|
||||||
|
|
||||||
|
let spawningFish = lanternFishSim[0]
|
||||||
|
|
||||||
|
# decrease all fish by one
|
||||||
|
for day in 0 .. FISH_DAYS-1:
|
||||||
|
lanternFishSim[day] = lanternFishSim[day+1]
|
||||||
|
|
||||||
|
# set the newly spawned fish
|
||||||
|
lanternFishSim[FISH_DAYS] = spawningFish
|
||||||
|
|
||||||
|
# 6 day left fish now only have the 7 day fish from
|
||||||
|
# the last round. We need to add the spawning fish
|
||||||
|
# to that count because they are refreshed at 6 days
|
||||||
|
lanternFishSim[6] += spawningFish
|
||||||
|
|
||||||
|
echo "NumFish: ", lanternFishSim.foldl(a + b)
|
||||||
|
|
||||||
|
main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue