diff --git a/kt/aoc2024-exe/src/blockingBench/kotlin/com/github/ephemient/aoc2024/exe/Day6Bench.kt b/kt/aoc2024-exe/src/blockingBench/kotlin/com/github/ephemient/aoc2024/exe/Day6Bench.kt index 23485c0..abddedd 100644 --- a/kt/aoc2024-exe/src/blockingBench/kotlin/com/github/ephemient/aoc2024/exe/Day6Bench.kt +++ b/kt/aoc2024-exe/src/blockingBench/kotlin/com/github/ephemient/aoc2024/exe/Day6Bench.kt @@ -27,7 +27,7 @@ class Day6Bench { } @Benchmark - fun both(bh: Blackhole) = runBlocking(Dispatchers.Default) { + fun solve(bh: Blackhole) = runBlocking(Dispatchers.Default) { val day6 = Day6(input) bh.consume(day6.part1()) bh.consume(day6.part2()) diff --git a/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day1Bench.kt b/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day1Bench.kt index 0292d0e..4504d81 100644 --- a/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day1Bench.kt +++ b/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day1Bench.kt @@ -23,7 +23,7 @@ class Day1Bench { fun part2() = Day1(input).part2() @Benchmark - fun both(bh: Blackhole) { + fun solve(bh: Blackhole) { val day1 = Day1(input) bh.consume(day1.part1()) bh.consume(day1.part2()) diff --git a/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day2Bench.kt b/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day2Bench.kt index 560d66e..f7d6e4c 100644 --- a/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day2Bench.kt +++ b/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day2Bench.kt @@ -23,7 +23,7 @@ class Day2Bench { fun part2() = Day2(input).part2() @Benchmark - fun both(bh: Blackhole) { + fun solve(bh: Blackhole) { val day2 = Day2(input) bh.consume(day2.part1()) bh.consume(day2.part2()) diff --git a/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day3Bench.kt b/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day3Bench.kt index c6df9a5..df1c63a 100644 --- a/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day3Bench.kt +++ b/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day3Bench.kt @@ -23,7 +23,7 @@ class Day3Bench { fun part2() = Day3(input).part2() @Benchmark - fun both(bh: Blackhole) { + fun solve(bh: Blackhole) { val day3 = Day3(input) bh.consume(day3.part1()) bh.consume(day3.part2()) diff --git a/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day4Bench.kt b/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day4Bench.kt index 356d016..97a9359 100644 --- a/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day4Bench.kt +++ b/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day4Bench.kt @@ -23,7 +23,7 @@ class Day4Bench { fun part2() = Day4(input).part2() @Benchmark - fun both(bh: Blackhole) { + fun solve(bh: Blackhole) { val day4 = Day4(input) bh.consume(day4.part1()) bh.consume(day4.part2()) diff --git a/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day5Bench.kt b/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day5Bench.kt index 6a94f92..de8e914 100644 --- a/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day5Bench.kt +++ b/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day5Bench.kt @@ -23,7 +23,7 @@ class Day5Bench { fun part2() = Day5(input).part2() @Benchmark - fun both(bh: Blackhole) { + fun solve(bh: Blackhole) { val day5 = Day5(input) bh.consume(day5.part1()) bh.consume(day5.part2()) diff --git a/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day7Bench.kt b/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day7Bench.kt index 236c6a3..db0c6a0 100644 --- a/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day7Bench.kt +++ b/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day7Bench.kt @@ -23,7 +23,7 @@ class Day7Bench { fun part2() = Day7(input).part2() @Benchmark - fun both(bh: Blackhole) { + fun solve(bh: Blackhole) { val day7 = Day7(input) bh.consume(day7.part1()) bh.consume(day7.part2()) diff --git a/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day7.kt b/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day7.kt index 5167672..83342c4 100644 --- a/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day7.kt +++ b/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day7.kt @@ -6,38 +6,32 @@ class Day7(input: String) { lhs.toLong() to rhs.split(' ').map { it.toLong() } }.toList() - fun part1() = equations.sumOf { equation -> - val stack = mutableListOf(equation) + private fun solve(op: suspend SequenceScope.(Long, Long) -> Unit) = equations.sumOf { + val stack = mutableListOf(it) while (stack.isNotEmpty()) { val (x, values) = stack.removeLast() val y = values.last() if (values.size == 1) { - if (x == y) return@sumOf equation.first else continue + if (x == y) return@sumOf it.first else continue } val rest = values.subList(0, values.lastIndex) - if (x >= y) stack.add(x - y to rest) - if (x % y == 0L) stack.add(x / y to rest) + sequence { op(x, y) }.mapTo(stack) { it to rest } } 0 } - fun part2() = equations.sumOf { equation -> - val stack = mutableListOf(equation) - while (stack.isNotEmpty()) { - val (x, values) = stack.removeLast() - val y = values.last() - if (values.size == 1) { - if (x == y) return@sumOf equation.first else continue - } - val rest = values.subList(0, values.lastIndex) - if (x >= y) stack.add(x - y to rest) - if (x % y == 0L) stack.add(x / y to rest) - if (x > y) { - var d = 10L - while (d <= y) d *= 10 - if (x % d == y) stack.add(x / d to rest) - } + fun part1() = solve { x, y -> + if (x >= y) yield(x - y) + if (x % y == 0L) yield(x / y) + } + + fun part2() = solve { x, y -> + if (x >= y) yield(x - y) + if (x % y == 0L) yield(x / y) + if (x > y) { + var d = 10L + while (d <= y) d *= 10 + if (x % d == y) yield(x / d) } - 0 } }