forked from matelaszlo/advent-of-code-scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12.scala
29 lines (22 loc) · 986 Bytes
/
Day12.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
package com.lmat.adventofcode.year2015
import com.lmat.adventofcode.SimpleCommonPuzzle
import com.lmat.util.Files.readResource
import com.lmat.util._
object Day12 extends SimpleCommonPuzzle[String, Int, Int] {
override def parse(resource: String): String = readResource(resource).head
override def part1(input: String): Int = extractAllNumbers(input).sum
def extractAllNumbers(input: String): Seq[Int] =
"[-0-9]+".r.findAllIn(input).toSeq.map(_.toInt)
override def part2(input: String): Int =
sumAllInts(Json.parse(input).get)
def sumAllInts(json: Json): Int = json match {
case JsonIntValue(value) => value
case JsonStringValue(_) => 0
case JsonArray(elements) => elements.map(sumAllInts).sum
case JsonObject(fields) => if(fields.values.exists(isRed)) 0 else fields.values.map(sumAllInts).sum
}
def isRed(json: Json): Boolean = json match {
case JsonStringValue(value) => value == "red"
case _ => false
}
}