Skip to content

Commit

Permalink
Deduplicate parts in 2024 day 7
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 7, 2024
1 parent bd44e54 commit a47d3f2
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions src/main/scala/eu/sim642/adventofcode2024/Day7.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package eu.sim642.adventofcode2024

import eu.sim642.adventofcodelib.IntegralImplicits._

object Day7 {

case class Equation(testValue: Long, numbers: Seq[Int])
Expand All @@ -15,47 +17,44 @@ object Day7 {
}
}

object Part1 extends Part {
override def isSolvable(equation: Equation): Boolean = {
def unAdd(testValue: Long, number: Int): Option[Long] =
if (testValue > number) Some(testValue - number) else None

def helper(testValue: Long, numbers: List[Int]): Boolean = numbers match {
case Nil => ???
case List(number) => number == testValue
case number :: numbers =>
testValue % number == 0 && helper(testValue / number, numbers) || helper(testValue - number, numbers)
}
def unMultiply(testValue: Long, number: Int): Option[Long] = testValue /! number

helper(equation.testValue, equation.numbers.view.reverse.toList)
}
def unConcatenate(testValue: Long, number: Int): Option[Long] = {
val testValueStr = testValue.toString
val numberStr = number.toString
if (testValueStr.length > numberStr.length && testValueStr.endsWith(numberStr))
Some(testValueStr.dropRight(numberStr.length).toLong)
else
None
}

object Part2 extends Part {
trait RightToLeftPart extends Part {
val unOperators: Seq[(Long, Int) => Option[Long]]

override def isSolvable(equation: Equation): Boolean = {

def helper(testValue: Long, numbers: List[Int]): Boolean = numbers match {
case Nil => ???
case Nil => throw new IllegalAccessException("illegal empty equation")
case List(number) => number == testValue
case number :: newNumbers =>
testValue % number == 0 && helper(testValue / number, newNumbers) || helper(testValue - number, newNumbers) || concatHelper(testValue, numbers)
}

def concatHelper(testValue: Long, numbers: List[Int]): Boolean = numbers match {
case number :: newNumbers =>
val testValueStr = testValue.toString
val numberStr = number.toString
if (testValueStr.length > numberStr.length && testValueStr.endsWith(numberStr)) {
val newTestValue = testValueStr.dropRight(numberStr.length).toLong
helper(newTestValue, newNumbers)
}
else
false
case _ => false
unOperators.exists(_(testValue, number).exists(helper(_, newNumbers)))
}

helper(equation.testValue, equation.numbers.view.reverse.toList)
}
}

object Part1 extends RightToLeftPart {
override val unOperators: Seq[(Long, Int) => Option[Long]] = Seq(unMultiply, unAdd)
}

object Part2 extends RightToLeftPart {
override val unOperators: Seq[(Long, Int) => Option[Long]] = Seq(unMultiply, unAdd, unConcatenate)
}

def parseEquation(s: String): Equation = s match {
case s"$testValue: $numbers" =>
Equation(testValue.toLong, numbers.split(" ").map(_.toInt).toSeq)
Expand Down

0 comments on commit a47d3f2

Please sign in to comment.