-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day7Bench.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.github.ephemient.aoc2024.exe | ||
|
||
import com.github.ephemient.aoc2024.Day7 | ||
import kotlinx.benchmark.Benchmark | ||
import kotlinx.benchmark.Blackhole | ||
import kotlinx.benchmark.Scope | ||
import kotlinx.benchmark.Setup | ||
import kotlinx.benchmark.State | ||
|
||
@State(Scope.Benchmark) | ||
class Day7Bench { | ||
private lateinit var input: String | ||
|
||
@Setup | ||
fun setup() { | ||
input = getDayInput(7) | ||
} | ||
|
||
@Benchmark | ||
fun part1() = Day7(input).part1() | ||
|
||
@Benchmark | ||
fun part2() = Day7(input).part2() | ||
|
||
@Benchmark | ||
fun both(bh: Blackhole) { | ||
val day7 = Day7(input) | ||
bh.consume(day7.part1()) | ||
bh.consume(day7.part2()) | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day7.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
class Day7(input: String) { | ||
private val equations = input.lineSequence().filter { it.isNotEmpty() }.map { line -> | ||
val (lhs, rhs) = line.split(": ", limit = 2) | ||
lhs.toLong() to rhs.split(' ').map { it.toLong() } | ||
}.toList() | ||
|
||
fun part1() = 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) | ||
} | ||
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) | ||
} | ||
} | ||
0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day7Test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class Day7Test { | ||
@Test | ||
fun part1() { | ||
assertEquals(3749L, Day7(example).part1()) | ||
} | ||
|
||
@Test | ||
fun part2() { | ||
assertEquals(11387L, Day7(example).part2()) | ||
} | ||
|
||
companion object { | ||
private val example = | ||
""" | ||
|190: 10 19 | ||
|3267: 81 40 27 | ||
|83: 17 5 | ||
|156: 15 6 | ||
|7290: 6 8 6 15 | ||
|161011: 16 10 13 | ||
|192: 17 8 14 | ||
|21037: 9 7 18 13 | ||
|292: 11 6 16 20 | ||
|""".trimMargin() | ||
} | ||
} |