-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay09.scala
37 lines (29 loc) · 1.27 KB
/
Day09.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
27
28
29
30
31
32
33
34
35
36
37
package com.lmat.adventofcode.year2017
import com.lmat.adventofcode.SimpleCommonPuzzle
import com.lmat.util.Files.readResource
import scala.annotation.tailrec
object Day09 extends SimpleCommonPuzzle[String, Int, Int] {
override def parse(resource: String): String = readResource(resource).head
/**
* Every groups score is equal to its nesting level (starting from 1)
* The level increases with every '{' and decreases with every '}'
* We need to remove all garbage first to avoid counting invalid braces
*/
override def part1(stream: String): Int = {
@tailrec
def calculateScore(remaining: List[Char], level: Int, acc: Int): Int = remaining match {
case List() => acc
case '{' :: rest => calculateScore(rest, level + 1, acc + level)
case '}' :: rest => calculateScore(rest, level - 1, acc)
case _ :: rest => calculateScore(rest, level, acc)
}
val simplified = removeGarbage(stream)
calculateScore(simplified.toList, 1, 0)
}
override def part2(stream: String): Int =
"<.*?>".r.findAllIn(removeCancelled(stream)).map(_.length - 2).sum
def removeGarbage(stream: String): String =
removeCancelled(stream).replaceAll("<.*?>", "")
def removeCancelled(stream: String): String =
stream.replaceAll("!.", "")
}