Skip to content

Commit

Permalink
Day 7: Bridge Repair
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 7, 2024
1 parent dbb38a1 commit 798afb1
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ Development occurs in language-specific directories:
|[Day4.hs](hs/src/Day4.hs)|[Day4.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day4.kt)|[day4.py](py/aoc2024/day4.py)|[day4.rs](rs/src/day4.rs)|
|[Day5.hs](hs/src/Day5.hs)|[Day5.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day5.kt)|[day5.py](py/aoc2024/day5.py)|[day5.rs](rs/src/day5.rs)|
|[Day6.hs](hs/src/Day6.hs)|[Day6.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day6.kt)|[day6.py](py/aoc2024/day6.py)|[day6.rs](rs/src/day6.rs)|
|[Day7.hs](hs/src/Day7.hs)||||
|[Day7.hs](hs/src/Day7.hs)|[Day7.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day7.kt)||[|
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())
}
}
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ val days: List<Day> = listOf(
Day(4, ::Day4, Day4::part1, Day4::part2),
Day(5, ::Day5, Day5::part1, Day5::part2),
Day(6, ::Day6, Day6::part1, Day6::part2),
Day(7, ::Day7, Day7::part1, Day7::part2),
)

data class Day(
Expand Down
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()
}
}

0 comments on commit 798afb1

Please sign in to comment.