From 1296dbb9cda0b332441b95eadc188fbb290f999a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20R=C3=B8dland?= Date: Tue, 12 Dec 2023 22:01:29 +0100 Subject: [PATCH] 2023 - Day12 - part 1 - moved time consuming part to part1 --- .../kotlin/no/rodland/advent_2023/Day12.kt | 42 +++++++------------ .../no/rodland/advent_2023/Day12Test.kt | 5 +-- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/src/main/kotlin/no/rodland/advent_2023/Day12.kt b/src/main/kotlin/no/rodland/advent_2023/Day12.kt index fa951ba..7ec74ed 100644 --- a/src/main/kotlin/no/rodland/advent_2023/Day12.kt +++ b/src/main/kotlin/no/rodland/advent_2023/Day12.kt @@ -1,60 +1,48 @@ package no.rodland.advent_2023 import no.rodland.advent.Day -import kotlin.math.pow // template generated: 12/12/2023 // Fredrik Rødland 2023 -class Day12(val input: List) : Day, List>>> { +class Day12(val input: List) : Day>>> { private val parsed = input.parse() override fun partOne(): Int { - val hei = parsed.map { (candidates, numbers) -> - candidates + return parsed.sumOf { (candidates, numbers) -> + expand(candidates) .map { candidate -> candidate.split("\\.+".toRegex()).filterNot { it.isEmpty() } } .filter { it.size == numbers.size } .map { strings -> strings.map { it.length } } .count { it == numbers } } - return hei.sum() } override fun partTwo(): Long { return 2 } - data class Springs(val candidates: String, val lengths: List) fun expand(input: String): List { - val count = input.count { it == '?' } - if (count == 0) { - return listOf(input) + fun String.replaceBoolean(): List = map { char -> + if (char == '0') '.' + else '#' } - val numChars = 2.0.pow(count).toInt() - val replacement = (0..(numChars - 1)).map { n -> - val bin = n.toString(2).padStart(count, '0') - bin.map { char -> - if (char == '0') '.' - else '#' - }.joinToString("") - }.map { - var replaced = input - for (char in it) { - replaced = replaced.replaceFirst("?", char.toString()) - } - replaced + + val count = input.count { it == '?' } + val numChars = 1 shl count // 2^count + return (0.. + boolean.toString(2).padStart(count, '0').replaceBoolean() + }.map { mask -> + mask.fold(input) { acc: String, c: Char -> acc.replaceFirst("?", c.toString()) } } - return replacement } - override fun List.parse(): List, List>> { - + override fun List.parse(): List>> { return map { line -> val (first, second) = line.split(" ") - val ints = second.split(",").map { it.toInt() } - expand(first) to ints + first to second.split(",").map { it.toInt() } } } diff --git a/src/test/kotlin/no/rodland/advent_2023/Day12Test.kt b/src/test/kotlin/no/rodland/advent_2023/Day12Test.kt index dc5c504..ad4c791 100644 --- a/src/test/kotlin/no/rodland/advent_2023/Day12Test.kt +++ b/src/test/kotlin/no/rodland/advent_2023/Day12Test.kt @@ -29,10 +29,8 @@ internal class Day12Test { resultTwo, { Day12(data12) }, { Day12(test12) }, - numInitLive = 0, numTestPart1 = 1, - numTestPart2 = 2, - + numTestPart2 = 1, ) @Nested @@ -110,6 +108,7 @@ internal class Day12Test { } @Test + @Slow(10000) fun `12,1,live,1`() { report(test.livePart1) }