Simplify
This commit is contained in:
parent
6761e4725d
commit
13042e1de2
3 changed files with 7 additions and 5 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue