Skip to content

Commit

Permalink
2023 - Day12 - part 1 - moved time consuming part to part1
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 12, 2023
1 parent c33a8c9 commit 1296dbb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 30 deletions.
42 changes: 15 additions & 27 deletions src/main/kotlin/no/rodland/advent_2023/Day12.kt
Original file line number Diff line number Diff line change
@@ -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<String>) : Day<Int, Long, List<Pair<List<String>, List<Int>>>> {
class Day12(val input: List<String>) : Day<Int, Long, List<Pair<String, List<Int>>>> {

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<Int>)

fun expand(input: String): List<String> {
val count = input.count { it == '?' }
if (count == 0) {
return listOf(input)
fun String.replaceBoolean(): List<Char> = 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..<numChars).map { boolean ->
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<String>.parse(): List<Pair<List<String>, List<Int>>> {

override fun List<String>.parse(): List<Pair<String, List<Int>>> {
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() }
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/test/kotlin/no/rodland/advent_2023/Day12Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ internal class Day12Test {
resultTwo,
{ Day12(data12) },
{ Day12(test12) },
numInitLive = 0,
numTestPart1 = 1,
numTestPart2 = 2,

numTestPart2 = 1,
)

@Nested
Expand Down Expand Up @@ -110,6 +108,7 @@ internal class Day12Test {
}

@Test
@Slow(10000)
fun `12,1,live,1`() {
report(test.livePart1)
}
Expand Down

0 comments on commit 1296dbb

Please sign in to comment.