Skip to content

Commit cb5b674

Browse files
committed
Tidy
1 parent 7a1886b commit cb5b674

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

src/main/scala/AdventOfCode2022/Day21.scala

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,38 @@
11
package AdventOfCode2022
22

33
object Day21:
4-
def parse(input: Seq[String]): collection.mutable.Map[String, () => Long] =
4+
def parse(input: Seq[String], part2: Boolean): collection.mutable.Map[String, () => Long] =
55
val monkeys = collection.mutable.Map[String, () => Long]()
6-
def compute(name: String): Long = monkeys(name)()
7-
86
input.foreach { line =>
97
val Array(name, rest: _*) = line.split("[: ]+"): @unchecked
108
monkeys(name) = rest match
119
case Seq(number) => () => number.toLong
12-
case Seq(left, op, right) => op match
13-
case "+" => () => compute(left) + compute(right)
14-
case "-" => () => compute(left) - compute(right)
15-
case "*" => () => compute(left) * compute(right)
16-
case "/" => () => compute(left) / compute(right)
10+
case Seq(left, operation, right) => operation match
11+
case _ if name == "root" && part2 => () => (monkeys(left)() - monkeys(right)()).abs
12+
case "+" => () => monkeys(left)() + monkeys(right)()
13+
case "-" => () => monkeys(left)() - monkeys(right)()
14+
case "*" => () => monkeys(left)() * monkeys(right)()
15+
case "/" => () => monkeys(left)() / monkeys(right)()
1716
}
18-
1917
monkeys
20-
end parse
2118

22-
def part1(input: Seq[String]): Long = parse(input)("root")()
19+
def part1(input: Seq[String]): Long = parse(input, false)("root")()
2320

2421
def part2(input: Seq[String]): Long =
25-
val monkeys = parse(input)
26-
var start = 3_000_000_000_000L
27-
var middle = 0L
28-
var end = 4_000_000_000_000L
22+
val monkeys = parse(input, true)
2923

30-
while (start < end) do {
31-
val middle = (start + end) / 2
24+
def check(n: Long): Long =
25+
monkeys("humn") = () => n
26+
monkeys("root")()
3227

33-
monkeys("humn") = () => middle
34-
val result = monkeys("lzfc")()
35-
val target = monkeys("qrgn")()
36-
37-
if result < target then end = middle - 1
38-
else if result > target then start = middle + 1
39-
else return middle
40-
}
28+
def helper(prev: Long, n: Long, step: Long): Long =
29+
val next = n + step
30+
val result = check(next)
31+
if result == 0 then next
32+
else if result < prev then helper(result, next, step)
33+
else helper(result, next, step / -2)
4134

42-
-1
35+
helper(check(0), 0, 1 << 60)
4336
end part2
4437

4538
def main(args: Array[String]): Unit =

src/test/scala/AdventOfCode2022/Day21Suite.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ class Day21Suite extends AnyFunSuite:
2323
test("Part 1 should handle sample input correctly") {
2424
assert(Day21.part1(sample) == 152)
2525
}
26+
27+
test("Part 2 should handle sample input correctly") {
28+
assert(Day21.part2(sample) == 302)
29+
}

0 commit comments

Comments
 (0)