Skip to content

Commit f1f52f3

Browse files
committed
Add derivative solution to 2022 day 21 part 2
1 parent f978e3d commit f1f52f3

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/main/scala/eu/sim642/adventofcode2022/Day21.scala

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,44 @@ object Day21 {
170170
}
171171
}
172172

173+
/**
174+
* Solution, which finds the derivative w.r.t. humn to get the slope
175+
* and then solves linear equation to find humn.
176+
* Assumes expression is linear w.r.t. humn.
177+
*/
178+
object DerivativePart2Solution extends Part2Solution {
179+
180+
override def findHumn(monkeys: Monkeys): Long = {
181+
val humnMonkeys = makeHumnMonkeys(monkeys) + (humn -> Job.Number(0)) // evaluate at 0
182+
val evalName = makeEvalName[BigDecimal](humnMonkeys).andThen(_.get) // always get, because all variables set
183+
184+
def deriveName(name: String): BigDecimal = {
185+
if (name == humn)
186+
1
187+
else
188+
deriveJob(humnMonkeys(name))
189+
}
190+
191+
def deriveJob(job: Job): BigDecimal = job match {
192+
case Job.Number(_) => 0
193+
case Job.Operation(lhs, op, rhs) =>
194+
op match {
195+
case Op.Add => deriveName(lhs) + deriveName(rhs)
196+
case Op.Sub => deriveName(lhs) - deriveName(rhs)
197+
case Op.Mul =>
198+
deriveName(lhs) * evalName(rhs) + evalName(lhs) * deriveName(rhs)
199+
case Op.Div =>
200+
(deriveName(lhs) * evalName(rhs) - evalName(lhs) * deriveName(rhs)) / (evalName(rhs) * evalName(rhs))
201+
}
202+
}
203+
204+
val y0 = evalName(root)
205+
val dy0 = deriveName(root) // derivative at 0
206+
val x0 = y0 / -dy0
207+
x0.setScale(0, BigDecimal.RoundingMode.HALF_UP).longValue
208+
}
209+
}
210+
173211

174212
def parseMonkey(s: String): (String, Job) = s match {
175213
case s"$name: $lhs $opStr $rhs" =>

src/test/scala/eu/sim642/adventofcode2022/Day21Test.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Day21Test extends Suites(
1010
new InvertPart2SolutionTest,
1111
new BinarySearchPart2SolutionTest,
1212
new LinearPart2SolutionTest,
13+
new DerivativePart2SolutionTest,
1314
)
1415

1516
object Day21Test {
@@ -58,4 +59,6 @@ object Day21Test {
5859
class BinarySearchPart2SolutionTest extends Part2SolutionTest(BinarySearchPart2Solution)
5960

6061
class LinearPart2SolutionTest extends Part2SolutionTest(LinearPart2Solution)
62+
63+
class DerivativePart2SolutionTest extends Part2SolutionTest(DerivativePart2Solution)
6164
}

0 commit comments

Comments
 (0)