advent-of-code/2021/seven.nim

18 lines
546 B
Nim
Raw Normal View History

2021-12-07 11:46:57 -06:00
import ./common, std/[strutils, sequtils, strformat, sugar]
2021-12-07 10:06:34 -06:00
2021-12-07 11:46:57 -06:00
proc crabFuel(c: seq[int]): int =
(0..c.foldl(max(a, b))).toSeq()
.reduce((r,t) => min(c.foldl(a + abs(b - t), 0), r), high(int))
2021-12-07 10:06:34 -06:00
2021-12-07 11:13:30 -06:00
proc triangleNumber(n: int): int = int((n * (n + 1)) / 2)
2021-12-07 10:10:13 -06:00
2021-12-07 11:46:57 -06:00
proc crabMoreFuel(c: seq[int]): int =
(0..c.foldl(max(a, b))).toSeq()
.reduce((r,t) => min(c.foldl(a + abs(b - t).triangleNumber(), 0), r), high(int))
2021-12-07 10:09:52 -06:00
2021-12-07 11:46:57 -06:00
doDay(7,
(day) => day.loadInputText().split(',').map(parseInt),
crabFuel,
crabMoreFuel,
@[16,1,2,0,4,2,7,1,2,14], 37, 168)