From da61289cbb7c79e891f3b0200cf2342dadfc50ad Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Tue, 7 Dec 2021 12:01:34 -0600 Subject: [PATCH] Ok i'm done DRYing --- 2021/seven.nim | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/2021/seven.nim b/2021/seven.nim index 78c1038..dc4d670 100644 --- a/2021/seven.nim +++ b/2021/seven.nim @@ -1,17 +1,9 @@ import ./common, std/[strutils, sequtils, strformat, sugar] -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 crabFuel(c: seq[int], f = (n: int) => n): int = + toSeq((1..c.foldl(max(a, b)))).reduce((r,t) => min(c.foldl(a + f(abs(b - t)), 0), r), high(int)) -proc triangleNumber(n: int): int = int((n * (n + 1)) / 2) - -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, - (day) => day.loadInputText().split(',').map(parseInt), - crabFuel, - crabMoreFuel, +doDay(7, (n) => n.loadInputText().split(',').map(parseInt), + (n) => n.crabFuel(), + (n) => n.crabFuel((c: int) => (c * (c + 1)) div 2), @[16,1,2,0,4,2,7,1,2,14], 37, 168)