advent-of-code/2021/seven.nim

19 lines
669 B
Nim
Raw Normal View History

2021-12-07 11:13:30 -06:00
import std/[strutils, sequtils, strformat]
2021-12-07 10:06:34 -06:00
import ./common
2021-12-07 11:13:30 -06:00
proc crabFuel(input: string): int =
let crabs = input.split(',').map(parseInt)
2021-12-07 10:06:34 -06:00
result = int.high()
2021-12-07 11:13:30 -06:00
var maxCrab = crabs.foldl(max(a, b), 0)
for t in 0..maxCrab: result = min(crabs.foldl(a + abs(b - t), 0), result)
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:13:30 -06:00
proc crabMoreFuel(input: string): int =
let crabs = input.split(',').map(parseInt)
2021-12-07 10:09:52 -06:00
result = int.high()
2021-12-07 11:13:30 -06:00
var maxCrab = crabs.foldl(max(a, b), 0)
for t in 0..maxCrab: result = min(crabs.foldl(a + (b - t).abs().triangleNumber(), 0), result)
2021-12-07 10:09:52 -06:00
2021-12-07 11:13:30 -06:00
doDay(7, loadInputText, crabFuel, crabMoreFuel, "16,1,2,0,4,2,7,1,2,14", 37, 168)