Some cleanup
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user