Skip to content

Day 07 completed. #9

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

Merged
merged 3 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
74 changes: 74 additions & 0 deletions src/main/kotlin/day07/day07.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Advent of Code 2024, Day 07.
// By Sebastian Raaphorst, 2024.

package day07

import common.day
import common.readInput
import java.math.BigInteger

data class Equation(val total: BigInteger, val numbers: List<BigInteger>) {
/**
* The numbers list is evaluated left-to-right, and entries can be joined with + or *,
* or if allowConcat is true, they can also be concatenated.
*/
private fun canBeMade(allowConcat: Boolean): Boolean {
fun aux(currTotal: BigInteger = numbers.first(),
remainingNumbers: List<BigInteger> = numbers.drop(1)): Boolean {
// currTotal can only ever increase, so if we already passed total, there is no
// reason to continue.
if (currTotal > total) return false

// If we have used up all the numbers and have achieved total, success.
if (remainingNumbers.isEmpty()) return currTotal == total

// Calculate all of the possible extensions.
val nextNumber = remainingNumbers.first()
val nextRemainingNumbers = remainingNumbers.drop(1)
return aux(currTotal + nextNumber, nextRemainingNumbers) ||
aux(currTotal * nextNumber, nextRemainingNumbers) ||
(allowConcat && aux("$currTotal$nextNumber".toBigInteger(), nextRemainingNumbers))
}
return aux()
}

fun canBeMadeWithPlusTimes() =
canBeMade(false)

fun canBeMadeWithPlusTimesConcat() =
canBeMade(true)

companion object {
fun parseLine(input: String): Equation {
val (total, remaining) = input.split(":")
val numbers = remaining
.trim()
.split(Regex("""\s+"""))
.map(String::toBigInteger)
return Equation(total.toBigInteger(), numbers)
}
}
}

fun parse(input: String): List<Equation> =
input.trim().lines().map(Equation::parseLine)

fun answer1(equations: List<Equation>): BigInteger =
equations.filter(Equation::canBeMadeWithPlusTimes).sumOf(Equation::total)

fun answer2(equations: List<Equation>): BigInteger =
equations.filter(Equation::canBeMadeWithPlusTimesConcat).sumOf(Equation::total)


fun main() {
val input = readInput({}::class.day()).trim()
val equations = parse(input)

println("--- Day 7: Bridge Repair ---")

// Part 1: 2437272016585
println("Part 1: ${answer1(equations)}")

// Part 2: 162987117690649
println("Part 2: ${answer2(equations)}")
}
Loading
Loading