From 01bf675081a3431980f6f8570a80a4a8146aa23b Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Tue, 14 Dec 2021 14:48:33 +0100 Subject: [PATCH] day(14): refactor do not keep separate functions for the same functionality with different count of steps Signed-off-by: Matej Focko --- src/year2021/day14/Day14.kt | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/src/year2021/day14/Day14.kt b/src/year2021/day14/Day14.kt index d9379f0..79ce3b1 100644 --- a/src/year2021/day14/Day14.kt +++ b/src/year2021/day14/Day14.kt @@ -55,21 +55,10 @@ fun String.toPolymerTemplate(): PolymerTemplate = template + (it to template.getOrDefault(it, 0) + 1) } -fun part1(input: List): Long { +fun run(input: List, steps: Int): Long { val polymer = Polymer(input.drop(2).toPolymerRules(), input.first()) - repeat(10) { - polymer.step() - } - - val elements = polymer.elements - return elements.maxOf { it.value } - elements.minOf { it.value } -} - -fun part2(input: List): Long { - val polymer = Polymer(input.drop(2).toPolymerRules(), input.first()) - - repeat(40) { + repeat(steps) { polymer.step() } @@ -81,9 +70,9 @@ fun main() { val sample = readInput(14, "sample") val input = readInput(14, "input") - check(part1(sample) == 1588.toLong()) - println(part1(input)) + check(run(sample, 10) == 1588.toLong()) + println(run(input, 10)) - check(part2(sample) == 2188189693529) - println(part2(input)) + check(run(sample, 40) == 2188189693529) + println(run(input, 40)) }