Some cleanup

This commit is contained in:
ctsk
2024-12-07 08:34:46 +01:00
parent d4dd6e8f8b
commit c0167d1bfe

View File

@@ -9,26 +9,29 @@ object Day07 extends Solver(7):
a * (m * 10) + b a * (m * 10) + b
class Searcher(target: Long, nums: Vector[Long], elephants: Boolean): class Searcher(target: Long, nums: Vector[Long], elephants: Boolean):
def search(acc: Long = nums(1), pos: Int = 2): Boolean = def search(acc: Long = nums(0), pos: Int = 1): Boolean =
if (pos >= nums.length) acc == target if (pos >= nums.length) return acc == target
else if (acc > target) false if (acc > target) return false
else search(acc + nums(pos), pos + 1)
search(acc + nums(pos), pos + 1) || search(acc * nums(pos), pos + 1) || search(acc * nums(pos), pos + 1)
|| (elephants && search(concat(acc, nums(pos)), pos + 1)) || (elephants && search(concat(acc, nums(pos)), pos + 1))
def run(input: os.ReadablePath): (Timings, Solution) = def run(input: os.ReadablePath): (Timings, Solution) =
val (pre_time, in) = timed { os.read.lines(input).map(longs).toVector } val (pre_time, in) = timed {
os.read.lines(input).map {
case s"$value: $rest" => (value.toLong, longs(rest))
}.toVector
}
val (p1_time, p1_solution) = timed { val (p1_time, p1_solution) = timed {
in in
.filter(nums => Searcher(nums(0), nums, false).search()) .filter((target, nums) => Searcher(target, nums, false).search())
.map(_.head) .map(_.head)
.sum .sum
} }
val (p2_time, p2_solution) = timed { val (p2_time, p2_solution) = timed {
in in
.filter(nums => Searcher(nums(0), nums, true).search()) .filter((target, nums) => Searcher(target, nums, true).search())
.map(_.head) .map(_.head)
.sum .sum
} }