Skip to content

Commit

Permalink
2024 - Day 19 - part 1 & 2
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 19, 2024
1 parent e46a227 commit b6c7276
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
19 changes: 15 additions & 4 deletions src/main/kotlin/no/rodland/advent_2024/Day19.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@ import no.rodland.advent.Day
// template generated: 19/12/2024
// Fredrik Rødland 2024

class Day19(val input: List<String>) : Day<Long, Long, Pair<List<String>, List<String>>> {
class Day19(val input: List<String>) : Day<Int, Long, Pair<List<String>, List<String>>> {

private val parsed = input.parse()
private val towels = parsed.first
private val designs = parsed.second

override fun partOne(): Long {
return 2
override fun partOne(): Int {
return designs.count { matches(it) > 0 }
}

override fun partTwo(): Long {
return 2
return designs.sumOf { matches(it) }
}

fun matches(str: String, matches: List<String> = emptyList(), cache: MutableMap<String, Long> = mutableMapOf()): Long {
if (str == "") {
return 1
}
return cache.getOrPut(str) {
towels
.filter { str.startsWith(it) }
.sumOf { matches(str.substringAfter(it), matches + it, cache) }
}
}

override fun List<String>.parse(): Pair<List<String>, List<String>> {
Expand Down
24 changes: 15 additions & 9 deletions src/test/kotlin/no/rodland/advent_2024/Day19Test.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.rodland.advent_2024

import no.rodland.advent.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import readFile
Expand All @@ -15,10 +16,10 @@ internal class Day19Test {
private val data19 = "2024/input_19.txt".readFile()
private val test19 = "2024/input_19_test.txt".readFile()

private val resultTestOne = 2L
private val resultTestTwo = 2L
private val resultOne = 2L
private val resultTwo = 2L
private val resultTestOne = 6
private val resultTestTwo = 16L
private val resultOne = 296
private val resultTwo = 619970556776002L

val test = defaultTestSuiteParseOnInit(
Day19(data19),
Expand All @@ -29,15 +30,12 @@ internal class Day19Test {
resultTwo,
{ Day19(data19) },
{ Day19(test19) },
numTestPart1 = 2,
numTestPart2 = 2,
)

@Nested
inner class Init {
@Test
fun `19,-,example,1`() {
report(AOCTest({ "123".toInt() }, Unit, 123, 5, "19".toInt(), Part.TWO, false, "example"))
}

@Test
fun `19,-,example,2`() {
report(test.initTest.copy())
Expand All @@ -48,6 +46,14 @@ internal class Day19Test {
report(test.initTest)
}

@Test
fun `19,-,test,substringAfter`() {
val str = "abcghiabcjkl"
assertEquals("NO_MATCH", str.substringAfter("sdjskdjks", "NO_MATCH"))
assertEquals("ghiabcjkl", str.substringAfter("abc", "NO_MATCH"))
assertEquals("", str.substringAfter("jkl", "NO_MATCH"))
}

@Test
fun `19,-,live,init`() {
report(test.initLive)
Expand Down

0 comments on commit b6c7276

Please sign in to comment.