From d0566593f6faa0c5df38288497feca319fd78791 Mon Sep 17 00:00:00 2001 From: scarf Date: Sun, 8 Dec 2024 12:48:49 +0900 Subject: [PATCH] test(2024/07): add test --- 2024/day07.scala | 15 ++++++++++----- 2024/day07.test.scala | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 2024/day07.test.scala diff --git a/2024/day07.scala b/2024/day07.scala index 238aa23..1b169e8 100644 --- a/2024/day07.scala +++ b/2024/day07.scala @@ -19,10 +19,15 @@ def getCombo(xs: List[Long], next: Op) = def solve(xss: Vector[(Long, List[Long])], op: Op) = xss.filter((target, xs) => getCombo(xs, op).exists(_ == target)).map(_._1).sum +def parse(input: String) = input.linesIterator.collect { case s"$target: $xs" => + (target.toLong, xs.split(" ").map(_.toLong).toList) +}.toVector + +def part1(y: Long)(x: Long) = List(x + y, x * y) +def part2(y: Long)(x: Long) = List(x + y, x * y, x || y) + @main def main() = - val input = readInput(this).getLines.collect { case s"$target: $xs" => - (target.toLong, xs.split(" ").map(_.toLong).toList) - }.toVector + val input = readInput(this).mkString |> parse - println(solve(input, (x) => (a) => List(a + x, a * x))) - println(solve(input, (x) => (a) => List(a + x, a * x, a || x))) + println(solve(input, part1)) + println(solve(input, part2)) diff --git a/2024/day07.test.scala b/2024/day07.test.scala new file mode 100644 index 0000000..6b8e490 --- /dev/null +++ b/2024/day07.test.scala @@ -0,0 +1,24 @@ +package `2024`.day07 + +import prelude.* +import munit.FunSuite + +val example = + """|190: 10 19 + |3267: 81 40 27 + |83: 17 5 + |156: 15 6 + |7290: 6 8 6 15 + |161011: 16 10 13 + |192: 17 8 14 + |21037: 9 7 18 13 + |292: 11 6 16 20""".stripMargin + +class Test extends FunSuite: + val input = example |> parse + + test("part 1"): + assertEquals(solve(input, part1), 3749L) + + test("part 2"): + assertEquals(solve(input, part2), 11387L)