diff --git a/2021/nim/common.nim b/2021/nim/common.nim index 3940de3..02992b3 100644 --- a/2021/nim/common.nim +++ b/2021/nim/common.nim @@ -38,14 +38,14 @@ template time*(i: string, body: untyped): untyped = when not defined(release): echo "NOTE: This is not a real measurement of performance. Compile in release mode with -d:release for best performance." -proc doDay*[T]( +proc doDay*[T, X]( day: int, inputLoader: int -> T, - part1: T -> int, - part2: T -> int, + part1: T -> X, + part2: T -> X, testInput: T, - expectedPart1: int, - expectedPart2: int): void = + expectedPart1: X, + expectedPart2: X): void = when not defined(release): var p1 = testInput.part1() @@ -61,6 +61,15 @@ proc doDay*[T]( time(&"Day {day} Part 2"): echo day.inputLoader().part2() +proc doDayX*[T, X]( + day: int, + inputLoader: int -> T, + part1: T -> X, + part2: T -> X, + testTuple: (T, X, X)): void = + let (tin, expectedPart1, expectedPart2) = testTuple + doDay(day, inputLoader, part1, part2, tin, expectedPart1, expectedPart2) + proc doDay*[T]( day: int, inputLoader: int -> T, diff --git a/2021/nim/day14.nim b/2021/nim/day14.nim index 6570729..61b591a 100644 --- a/2021/nim/day14.nim +++ b/2021/nim/day14.nim @@ -15,15 +15,23 @@ proc stepPolymer(polymer: string, combis: Table[(char, char), char]): string = result.add polymer[i] i += 1 -proc p1(input: Lines): int = +proc p1(input: Lines): uint64 = var (polymer, combis) = input.parse for i in 1..10: polymer = polymer.stepPolymer combis var pps = polymer.toCountTable.values.toSeq - max(pps) - min(pps) + uint64(max(pps) - min(pps)) -proc p2(input: Lines): int = - return 0 +proc p2(input: Lines): uint64 = + var (polymer, combis) = input.parse + for i in 1..40: + echo &"Step {i}" + polymer = polymer.stepPolymer combis + var t = initTable[char, uint64]() + for c in polymer: + inc t[c] + var pps = t.values.toSeq + max(pps) - min(pps) const input = """ NNCB @@ -45,4 +53,4 @@ BC -> B CC -> N CN -> C """.strip().split('\n').mapIt(it.strip) -doDay 14, n => n.loadInput, p1, p2, (input, 1588, 0) +doDayX 14, n => n.loadInput, p1, p2, (input, 1588'u64, 2188189693529'u64)