No case in regex!
This commit is contained in:
parent
ad788f4758
commit
c9395e793c
11
Day2.scala
11
Day2.scala
|
@ -14,12 +14,11 @@ case class Game(id: Int, hands: List[Hand]):
|
|||
object Parser extends RegexParsers:
|
||||
val num = """(0|[1-9]\d*)""".r ^^ { _.toInt }
|
||||
val update =
|
||||
num ~ """(red|green|blue)""".r ^^ { case (num ~ color) =>
|
||||
color match
|
||||
case "red" => (h: Hand) => h.copy(red = num)
|
||||
case "green" => (h: Hand) => h.copy(green = num)
|
||||
case "blue" => (h: Hand) => h.copy(blue = num)
|
||||
}
|
||||
num ~ (
|
||||
"red" ^^ (_ => (num: Int) => (h: Hand) => h.copy(red = num)) |
|
||||
"green" ^^ (_ => (num: Int) => (h: Hand) => h.copy(green = num)) |
|
||||
"blue" ^^ (_ => (num: Int) => (h: Hand) => h.copy(blue = num))
|
||||
) ^^ { case (num ~ f) => f(num) }
|
||||
val hand = rep1sep(update, ",").map(_.foldRight(Hand(0, 0, 0))(_(_)))
|
||||
|
||||
val game = "Game" ~ num ~ ":" ~ repsep(hand, ";") ^^ {
|
||||
|
|
Loading…
Reference in a new issue