Advent of Code 2022 day 7
12/07/2024, 5:00 AMMarcin Wisniowski
12/07/2024, 5:17 AMMarcin Wisniowski
12/07/2024, 5:18 AMJonathan Kolberg
12/07/2024, 5:23 AMMichael de Kaste
12/07/2024, 5:33 AMJonathan Kolberg
12/07/2024, 5:39 AMbj0
12/07/2024, 5:43 AMMichael de Kaste
12/07/2024, 5:46 AMMichael de Kaste
12/07/2024, 5:47 AMbj0
12/07/2024, 5:49 AMzipWithNext
bj0
12/07/2024, 5:49 AMbj0
12/07/2024, 5:50 AMOzioma Ogbe
12/07/2024, 5:58 AMimport java.io.File
fun main() {
val input = File("/Users/oogbe/IdeaProjects/AdventOfCodeSolutions/solutions/src/2024/input.txt").readText()
val lines = input.split("\n")
lines.mapNotNull {
val (expectedResult, testValues) = it.split(": ")
val values = testValues.split(" ").map { it.toInt() }
if(test(expectedResult.toLong(), values, 0, 0L)) expectedResult.toLong() else null
}.sum().also { println(it) }
}
fun test(expectedResult: Long, testValues: List<Int>, currentIndex: Int, currentResult: Long): Boolean {
val ans1 = currentResult * testValues[currentIndex]
val ans2 = currentResult + testValues[currentIndex]
val ans3 = (currentResult.toString() + testValues[currentIndex]).toLong()
if (currentIndex == testValues.lastIndex)
return ans1 == expectedResult || ans2 == expectedResult || ans3 == expectedResult
return test(expectedResult, testValues, currentIndex + 1, ans1) || test(
expectedResult,
testValues,
currentIndex + 1,
ans2
) || test(
expectedResult,
testValues,
currentIndex + 1,
ans3
)
}
Sergei Petunin
12/07/2024, 6:34 AMritesh
12/07/2024, 6:43 AMEndre Deak
12/07/2024, 7:11 AMfun main() {
solve("Bridge Repair") {
val eqsWithSolutions = mutableSetOf<String>()
fun calculate(vararg ops: String, skip: Set<String>? = null) =
(skip?.let { lines.minus(it) } ?: lines)
.sumOf { l ->
l.longs().let { numbers ->
ops.toSet()
.combinations(numbers.size - 2)
.firstOrNull { c ->
numbers.drop(2)
.zip(c)
.fold(numbers[1]) { acc, (n, op) ->
when (op) {
"+" -> acc + n
"*" -> acc * n
"|" -> "$acc$n".toLong()
else -> error("invalid operand")
}
} == numbers[0]
}
?.let { numbers[0] }
?.also { eqsWithSolutions.add(l) }
?: 0
}
}
part1() { calculate("+", "*") }
part2() { calculate("+", "*", "|", skip = eqsWithSolutions) }
}
}
ephemient
12/07/2024, 7:32 AMPetr Sýkora
12/07/2024, 7:33 AMPetr Sýkora
12/07/2024, 7:34 AMphldavies
12/07/2024, 7:35 AMPoisonedYouth
12/07/2024, 8:27 AMNeil Banman
12/07/2024, 9:21 AMNeil Banman
12/07/2024, 9:22 AMNeil Banman
12/07/2024, 9:32 AMMichael Böiers
12/07/2024, 9:49 AMCharles Flynn
12/07/2024, 10:27 AMPaul Woitaschek
12/07/2024, 10:43 AMDan Fingal-Surma
12/07/2024, 10:49 AMDan Fingal-Surma
12/07/2024, 10:56 AMDan Fingal-Surma
12/07/2024, 10:57 AMDan Fingal-Surma
12/07/2024, 11:02 AMJakub Gwóźdź
12/07/2024, 11:17 AMJakub Gwóźdź
12/07/2024, 11:24 AM5702958180383
took 949.625us
92612386119138
took 1.428042ms
Jaap Beetstra
12/07/2024, 11:41 AMtodd.ginsberg
12/07/2024, 2:04 PMFredrik Rødland
12/07/2024, 3:01 PMconcat
could be sped up a bit by calculating it instead of converting to string and back to long. Seems it can (by using guava), but not by very much:
private fun concat(l1: Long, l2: Long): Long {
val lengthOfL2 = floor(log10(l2.toDouble())).toInt() + 1
val multiplier = 10.0.pow(lengthOfL2).toLong()
return l1 * multiplier + l2
}
907ms
private fun concat(l1: Long, l2: Long): Long {
val lengthOfL2 = l2.toString().length
val multiplier = 10.0.pow(lengthOfL2).toLong()
return l1 * multiplier + l2
}
837ms
private fun concat(l1: Long, l2: Long): Long {
return "$l1$l2".toLong()
}
805ms
log10
and pow
from guava (to avoid converting between long and double)
private fun concat(l1: Long, l2: Long): Long {
val lengthOfL2 = log10(l2, RoundingMode.FLOOR) + 1
val multiplier = pow(10, lengthOfL2)
return l1 * multiplier + l2
}
673ms
Added later for reference (see comments below)
private fun concat(l1: Long, l2: Long): Long {
var t = l1
var l = l2
while (l > 0) {
t *= 10
l /= 10
}
return t + l2
}
772msFredrik Rødland
12/07/2024, 3:03 PM/3
, /2
etc - that would reduce the number of remaining tests dramatically. But then I just implemented the brute force variant, not seeing how to convert my thoughts into code. Thank you for actually showing how!Jakub Gwóźdź
12/07/2024, 4:04 PMprivate fun Long.concat(other: Long): Long {
var t = this
var l = other
while (l > 0) {
t *= 10
l /= 10
}
return t + other
}
but it was on par with toString/toLong. Which only shows how well optimized these methods are 🙂Fredrik Rødland
12/07/2024, 4:11 PMtodd.ginsberg
12/07/2024, 5:05 PMJakub Gwóźdź
12/07/2024, 5:18 PMphldavies
12/07/2024, 9:43 PMyear 2024 day 7 Default warmup (8 iterations) took 4.536497751s
year 2024 day 7 Inline warmup (126 iterations) took 8.797861427s
year 2024 day 7 Retrograde warmup (1514 iterations) took 813.961093ms
finished warmup after 14.156842791s
year 2024 day 7 part 1
Retrograde took 278.363us 👑: 3245122495150
Inline took 3.635531ms (13.06x): 3245122495150
Default took 11.622636ms (41.75x): 3245122495150
year 2024 day 7 part 2
Retrograde took 357.441us 👑: 105517128211543
Inline took 69.571818ms (194.64x): 105517128211543
Default took 484.402215ms (1355.19x): 105517128211543
Neil Banman
12/07/2024, 9:44 PMDan Fingal-Surma
12/07/2024, 11:42 PMNeil Banman
12/08/2024, 1:12 AMephemient
12/08/2024, 1:16 AM11111: 1 1 1 1
Jakub Gwóźdź
12/08/2024, 4:42 AM1111: 1 1 1 1
case is reducing from O(3^n) to O(2^n), but still - that's heavyNeil Banman
12/08/2024, 4:43 AM