-
Notifications
You must be signed in to change notification settings - Fork 3
/
Day13.scala
26 lines (21 loc) · 1.02 KB
/
Day13.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package AdventOfCode2022
object Day13:
def simplify(input: Seq[String]): Seq[String] = input.filter(_.nonEmpty).map(_.replace("10", "A"))
def compare(left: String, right: String): Boolean = (left.head, right.head) match
case (a, b) if a == b => compare(left.tail, right.tail)
case (']', _) => true
case (_, ']') => false
case ('[', b) => compare(left.tail, s"$b]" + right.tail)
case (a, '[') => compare(s"$a]" + left.tail, right.tail)
case (a, b) => a < b
def part1(input: Seq[String]): Int =
val pairs = simplify(input).grouped(2).zipWithIndex
pairs.collect { case (Seq(left, right), index) if compare(left, right) => index + 1 }.sum
def part2(input: Seq[String]): Int =
val dividers = Seq("[[2]]", "[[6]]")
val sorted = (simplify(input) ++ dividers).sortWith(compare)
dividers.map(sorted.indexOf(_) + 1).product
def main(args: Array[String]): Unit =
val data = io.Source.fromResource("AdventOfCode2022/Day13.txt").getLines().toSeq
println(part1(data))
println(part2(data))