diff --git a/2021/common.nim b/2021/common.nim index 052f581..475a32d 100644 --- a/2021/common.nim +++ b/2021/common.nim @@ -44,10 +44,13 @@ proc doDay*[T]( testInput: T, expectedPart1: int, expectedPart2: int): void = - let input = day.inputLoader() - time(&"day {day} part 1"): echo input.part1() - time(&"day {day} part 2"): echo input.part2() + time(&"day {day} part 1"): echo day.inputLoader().part1() + time(&"day {day} part 2"): echo day.inputLoader().part2() when not defined(release): doAssert testInput.part1() == expectedPart1 doAssert testInput.part2() == expectedPart2 + +proc reduce*[T](s: openArray[T], op: (T, T) -> T, init: T): T = + result = init + for n in s: result = op(result, n) diff --git a/2021/seven.nim b/2021/seven.nim index 1b27b0e..78c1038 100644 --- a/2021/seven.nim +++ b/2021/seven.nim @@ -1,18 +1,17 @@ -import std/[strutils, sequtils, strformat] -import ./common +import ./common, std/[strutils, sequtils, strformat, sugar] -proc crabFuel(input: string): int = - let crabs = input.split(',').map(parseInt) - result = int.high() - var maxCrab = crabs.foldl(max(a, b), 0) - for t in 0..maxCrab: result = min(crabs.foldl(a + abs(b - t), 0), result) +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)) proc triangleNumber(n: int): int = int((n * (n + 1)) / 2) -proc crabMoreFuel(input: string): int = - let crabs = input.split(',').map(parseInt) - result = int.high() - var maxCrab = crabs.foldl(max(a, b), 0) - for t in 0..maxCrab: result = min(crabs.foldl(a + (b - t).abs().triangleNumber(), 0), result) +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)) -doDay(7, loadInputText, crabFuel, crabMoreFuel, "16,1,2,0,4,2,7,1,2,14", 37, 168) +doDay(7, + (day) => day.loadInputText().split(',').map(parseInt), + crabFuel, + crabMoreFuel, + @[16,1,2,0,4,2,7,1,2,14], 37, 168)