advent-of-code/2021/nim/day7.nim

10 lines
383 B
Nim
Raw Normal View History

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