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, testInput: T,
expectedPart1: int, expectedPart1: int,
expectedPart2: int): void = expectedPart2: int): void =
let input = day.inputLoader() time(&"day {day} part 1"): echo day.inputLoader().part1()
time(&"day {day} part 1"): echo input.part1() time(&"day {day} part 2"): echo day.inputLoader().part2()
time(&"day {day} part 2"): echo input.part2()
when not defined(release): when not defined(release):
doAssert testInput.part1() == expectedPart1 doAssert testInput.part1() == expectedPart1
doAssert testInput.part2() == expectedPart2 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, std/[strutils, sequtils, strformat, sugar]
import ./common
proc crabFuel(input: string): int = proc crabFuel(c: seq[int]): int =
let crabs = input.split(',').map(parseInt) (0..c.foldl(max(a, b))).toSeq()
result = int.high() .reduce((r,t) => min(c.foldl(a + abs(b - t), 0), r), high(int))
var maxCrab = crabs.foldl(max(a, b), 0)
for t in 0..maxCrab: result = min(crabs.foldl(a + abs(b - t), 0), result)
proc triangleNumber(n: int): int = int((n * (n + 1)) / 2) proc triangleNumber(n: int): int = int((n * (n + 1)) / 2)
proc crabMoreFuel(input: string): int = proc crabMoreFuel(c: seq[int]): int =
let crabs = input.split(',').map(parseInt) (0..c.foldl(max(a, b))).toSeq()
result = int.high() .reduce((r,t) => min(c.foldl(a + abs(b - t).triangleNumber(), 0), r), high(int))
var maxCrab = crabs.foldl(max(a, b), 0)
for t in 0..maxCrab: result = min(crabs.foldl(a + (b - t).abs().triangleNumber(), 0), result)
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)