Advent of Code 2023 day 19
12/19/2024, 5:00 AMbj0
12/19/2024, 5:34 AMRenette Ros
12/19/2024, 5:34 AMMarcin Wisniowski
12/19/2024, 5:35 AMbj0
12/19/2024, 5:36 AMbj0
12/19/2024, 5:36 AMRenette Ros
12/19/2024, 5:37 AMreturn designs.count { possibleCount(patterns, it, mutableMapOf()) > 0 }
Renette Ros
12/19/2024, 5:38 AMrealizing the answer overflows an int...Had this same issue. But my final answer was negative so I knew immediately 😄
bj0
12/19/2024, 5:41 AMbj0
12/19/2024, 5:42 AMbj0
12/19/2024, 5:43 AMbj0
12/19/2024, 5:43 AMRenette Ros
12/19/2024, 5:45 AMbj0
12/19/2024, 5:46 AMbj0
12/19/2024, 5:46 AMbj0
12/19/2024, 5:46 AMJonathan Kolberg
12/19/2024, 5:47 AMbj0
12/19/2024, 5:47 AMbj0
12/19/2024, 5:48 AMRenette Ros
12/19/2024, 5:48 AMbj0
12/19/2024, 5:48 AMMarcin Wisniowski
12/19/2024, 5:55 AMJakub Gwóźdź
12/19/2024, 6:02 AMJakub Gwóźdź
12/19/2024, 6:02 AMMichael de Kaste
12/19/2024, 6:05 AMkingsley
12/19/2024, 6:16 AMephemient
12/19/2024, 6:35 AMephemient
12/19/2024, 6:39 AMperl -E'chomp($_=<>);s/, /|/g;$re=qr/^(?:$_)*$/;$a+=/./&&/$re/ while chomp($_=<>);say$a'
at my terminal)Dan Fingal-Surma
12/19/2024, 6:53 AM> 0
the new condition for ADan Fingal-Surma
12/19/2024, 7:00 AMDan Fingal-Surma
12/19/2024, 7:05 AMDan Fingal-Surma
12/19/2024, 7:06 AMDan Fingal-Surma
12/19/2024, 7:13 AMEndre Deak
12/19/2024, 7:14 AMDan Fingal-Surma
12/19/2024, 7:14 AMDan Fingal-Surma
12/19/2024, 7:15 AM124653528325
different ways to make itAnirudh
12/19/2024, 7:23 AMAnirudh
12/19/2024, 7:23 AMif (newStrings.isEmpty())
return false
return newStrings.any { it.isPossible() }
versus (b) the coolness of one
return newStrings.isNotEmpty() && newStrings.any { it.isPossible() }
🙃Dan Fingal-Surma
12/19/2024, 7:26 AMphldavies
12/19/2024, 7:27 AMphldavies
12/19/2024, 7:28 AMpattern.removePrefix(it)
is clearer than pattern.drop(it.length)
KDan Fingal-Surma
12/19/2024, 7:33 AMDan Fingal-Surma
12/19/2024, 7:35 AMreturn
inside getOrPut
… that’s burned me on more than one occasionMichael de Kaste
12/19/2024, 7:35 AMreturn@getOrPut
is your friendDan Fingal-Surma
12/19/2024, 7:35 AMAnirudh
12/19/2024, 7:35 AM"^$it"
instead of startsWith(it)
lolAnirudh
12/19/2024, 7:36 AMdone.count() + todo.sumOf { it.possibleWays() }
?HCP
12/19/2024, 7:37 AMDan Fingal-Surma
12/19/2024, 7:37 AMDan Fingal-Surma
12/19/2024, 7:37 AMHCP
12/19/2024, 7:38 AMDan Fingal-Surma
12/19/2024, 7:40 AMDan Fingal-Surma
12/19/2024, 7:40 AMAnirudh
12/19/2024, 7:42 AMDan Fingal-Surma
12/19/2024, 7:42 AMHCP
12/19/2024, 7:43 AMDan Fingal-Surma
12/19/2024, 7:43 AMval cache = mutableMapOf<CacheKey, U>()
fun myFun(args): U = cache.getOrPut(make CacheKey from args) {
// fn logic
}
Dan Fingal-Surma
12/19/2024, 7:44 AMval cache = mutableMapOf<T, U>()
fn myFun(arg: T): U = cache.getOrPut(arg) {
// logic
}
Dan Fingal-Surma
12/19/2024, 7:45 AMgetOrPut
takes one argJonathan Kolberg
12/19/2024, 7:46 AMHCP
12/19/2024, 7:47 AMDan Fingal-Surma
12/19/2024, 7:47 AMPair
also worksJaap Beetstra
12/19/2024, 7:48 AMMichael de Kaste
12/19/2024, 7:49 AMJaap Beetstra
12/19/2024, 7:58 AMdata class Day19(val towelTypes: List<String>, val designs: List<String>) {
private constructor(st: String, sd: String) : this(st.split(",").map { it.trim() }, sd.lines())
private constructor(input: List<String>) : this(input[0], input[1])
constructor(input: String) : this(input.paragraphs())
fun part1(): Int = designs.count { hasArrangement(it, mutableMapOf("" to true)) }
fun hasArrangement(design: String, memory: MutableMap<String, Boolean>): Boolean =
memory.getOrPut(design) {
towelTypes.filter { design.startsWith(it) }.any { hasArrangement(design.drop(it.length), memory) }
}
fun part2() = designs.sumOf { countArrangements(it, mutableMapOf("" to 1L)) }
fun countArrangements(design: String, memory: MutableMap<String, Long>): Long =
memory.getOrPut(design) {
towelTypes.filter { design.startsWith(it) }.sumOf { countArrangements(design.drop(it.length), memory) }
}
}
Anirudh
12/19/2024, 8:04 AMNorbert Kiesel
12/19/2024, 8:35 AMMichael Böiers
12/19/2024, 8:40 AMHCP
12/19/2024, 8:43 AMHCP
12/19/2024, 8:44 AMritesh
12/19/2024, 8:54 AMOzioma Ogbe
12/19/2024, 8:56 AMDan Fingal-Surma
12/19/2024, 8:59 AMDan Fingal-Surma
12/19/2024, 9:00 AMAnirudh
12/19/2024, 9:26 AMval (towels, patterns) = lines...
the top line of the input is towel patterns and the rest are designs but you used the top-line word for both parts 😛Dan Fingal-Surma
12/19/2024, 9:27 AMDan Fingal-Surma
12/19/2024, 9:28 AMval (patterns, designs) = lines[0].split(", ") to lines.drop(2)
nailed itJaap Beetstra
12/19/2024, 9:57 AMfun main() {
val input = java.io.File("src/Day19.txt").readLines()
input.drop(2).map { design ->
design.indices.drop(1).map(design::take).fold(listOf(1L)) { sums, part ->
sums + input[0].split(", ").filter(part::endsWith).sumOf { sums[sums.size - it.length] }
}.last()
}.run { println("${count { it > 0 }}\n${sum()}") }
}
Michael Böiers
12/19/2024, 10:40 AMfun main() {
val lines = generateSequence(::readLine).toList()
val cache = mutableMapOf("" to false)
fun String.match(): Boolean = cache.getOrPut(this) {
lines[0].split(", ").filter(::startsWith).any { equals(it) || removePrefix(it).match() }
}
println(lines.drop(2).count(String::match))
}
Michael Böiers
12/19/2024, 11:08 AMfun main() {
val lines = generateSequence(::readLine).toList()
val cache = mutableMapOf("" to 0L)
fun String.countCombinations(): Long = cache.getOrPut(this) {
lines[0].split(", ").filter(::startsWith).sumOf { if (this == it) 1 else removePrefix(it).countCombinations() }
}
println(lines.drop(2).sumOf(String::countCombinations))
}
Michael Böiers
12/19/2024, 11:15 AMfun main() {
val (towels, designs) = with(generateSequence(::readLine).toList()) { first().split(", ") to drop(2) }
val cache = mutableMapOf("" to 0L)
fun String.countCombinations(): Long = cache.getOrPut(this) {
towels.filter(::startsWith).sumOf { if (this == it) 1 else removePrefix(it).countCombinations() }
}
with(designs.map(String::countCombinations)) { listOf(count { it > 0 }, sum()).forEach(::println) }
}
Paul Woitaschek
12/19/2024, 7:57 PM