-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.sc
21 lines (17 loc) · 841 Bytes
/
day07.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import common.loadPackets
val input = loadPackets(List("day07.txt"))
.map({ case s"${sum}: ${rest}" =>
(BigInt(sum), rest.split("""\s+""").map(BigInt(_)).toList)
})
case class Algebra(operators: List[(BigInt, BigInt) => BigInt]):
def canBeTrue(desiredResult: BigInt, operands: List[BigInt], soFar: BigInt = 0): Boolean =
if soFar > desiredResult then false else operands match {
case Nil => desiredResult == soFar
case operand :: rest => operators.exists(op => canBeTrue(desiredResult, rest, op(soFar, operand)))
}
def calibrationResult: BigInt = input.filter({
case (result, operands) => canBeTrue(result, operands)
}).map(_._1).sum
def concat(a: BigInt, b: BigInt) = BigInt(s"${a}${b}")
val part1 = Algebra(List(_+_, _*_)).calibrationResult
val part2 = Algebra(List(_+_, _*_, concat)).calibrationResult