From c0167d1bfe115c980e76c6922055e38e22973733 Mon Sep 17 00:00:00 2001 From: ctsk <9384305+ctsk@users.noreply.github.com> Date: Sat, 7 Dec 2024 08:34:46 +0100 Subject: [PATCH] Some cleanup --- aoc/src/dev/ctsk/aoc/days/Day07.scala | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/aoc/src/dev/ctsk/aoc/days/Day07.scala b/aoc/src/dev/ctsk/aoc/days/Day07.scala index 6120a59..ad15635 100644 --- a/aoc/src/dev/ctsk/aoc/days/Day07.scala +++ b/aoc/src/dev/ctsk/aoc/days/Day07.scala @@ -9,26 +9,29 @@ object Day07 extends Solver(7): a * (m * 10) + b class Searcher(target: Long, nums: Vector[Long], elephants: Boolean): - def search(acc: Long = nums(1), pos: Int = 2): Boolean = - if (pos >= nums.length) acc == target - else if (acc > target) false - else - search(acc + nums(pos), pos + 1) || search(acc * nums(pos), pos + 1) + def search(acc: Long = nums(0), pos: Int = 1): Boolean = + if (pos >= nums.length) return acc == target + if (acc > target) return false + search(acc + nums(pos), pos + 1) + || search(acc * nums(pos), pos + 1) || (elephants && search(concat(acc, nums(pos)), pos + 1)) - 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 { in - .filter(nums => Searcher(nums(0), nums, false).search()) + .filter((target, nums) => Searcher(target, nums, false).search()) .map(_.head) .sum } val (p2_time, p2_solution) = timed { in - .filter(nums => Searcher(nums(0), nums, true).search()) + .filter((target, nums) => Searcher(target, nums, true).search()) .map(_.head) .sum }