Golf it a bit for no good reason

This commit is contained in:
Daniel Flanagan 2021-12-07 11:46:57 -06:00
parent bebcd21df8
commit 25e1c51fa4
Signed by: lytedev
GPG Key ID: 5B2020A0F9921EF4
2 changed files with 18 additions and 16 deletions

View File

@ -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)

View File

@ -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)