Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor out duplicated code #62

Merged
merged 2 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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
}
}
Loading