From 0f1e145b80813ae2331b7dac5ace0c589654ad2a Mon Sep 17 00:00:00 2001 From: tchojnacki Date: Thu, 11 Aug 2022 19:24:23 +0200 Subject: Move subproject to avoid IntelliJ module name issues --- 2021-kotlin/src/Day14.kt | 46 ---------------------------------------------- 1 file changed, 46 deletions(-) delete mode 100644 2021-kotlin/src/Day14.kt (limited to '2021-kotlin/src/Day14.kt') diff --git a/2021-kotlin/src/Day14.kt b/2021-kotlin/src/Day14.kt deleted file mode 100644 index 920ea9e..0000000 --- a/2021-kotlin/src/Day14.kt +++ /dev/null @@ -1,46 +0,0 @@ -object Day14 { - private fun getPolymerLetterCounts(input: List, iterations: Int): Long { - val template = input.first() - val rules = input.drop(2).associate { - val (pattern, replacement) = it.split(" -> ") - (pattern[0] to pattern[1]) to replacement.first() - } - - var pairCounts = template - .zipWithNext() - .groupingBy { it } - .eachCount() - .mapValues { (_, v) -> v.toLong() } - - repeat(iterations) { - val newCounts = mutableMapOf, Long>() - - pairCounts.forEach { (pair, count) -> - newCounts.merge(rules[pair]!! to pair.second, count, Long::plus) - newCounts.merge(pair.first to rules[pair]!!, count, Long::plus) - } - - pairCounts = newCounts - } - - val letterCounts = mutableMapOf() - pairCounts.forEach { (pair, count) -> letterCounts.merge(pair.second, count, Long::plus) } - letterCounts.merge(template.first(), 1, Long::plus) - - return letterCounts.values.let { it.maxOrNull()!! - it.minOrNull()!! } - } - - fun part1(input: List): Long = getPolymerLetterCounts(input, 10) - - fun part2(input: List): Long = getPolymerLetterCounts(input, 40) -} - -fun main() { - val testInput = readInputAsLines("Day14_test") - check(Day14.part1(testInput) == 1588L) - check(Day14.part2(testInput) == 2188189693529L) - - val input = readInputAsLines("Day14") - println(Day14.part1(input)) - println(Day14.part2(input)) -} -- cgit v1.2.3