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:
|
object Parser extends RegexParsers:
|
||||||
val num = """(0|[1-9]\d*)""".r ^^ { _.toInt }
|
val num = """(0|[1-9]\d*)""".r ^^ { _.toInt }
|
||||||
val update =
|
val update =
|
||||||
num ~ """(red|green|blue)""".r ^^ { case (num ~ color) =>
|
num ~ (
|
||||||
color match
|
"red" ^^ (_ => (num: Int) => (h: Hand) => h.copy(red = num)) |
|
||||||
case "red" => (h: Hand) => h.copy(red = num)
|
"green" ^^ (_ => (num: Int) => (h: Hand) => h.copy(green = num)) |
|
||||||
case "green" => (h: Hand) => h.copy(green = num)
|
"blue" ^^ (_ => (num: Int) => (h: Hand) => h.copy(blue = num))
|
||||||
case "blue" => (h: Hand) => h.copy(blue = num)
|
) ^^ { case (num ~ f) => f(num) }
|
||||||
}
|
|
||||||
val hand = rep1sep(update, ",").map(_.foldRight(Hand(0, 0, 0))(_(_)))
|
val hand = rep1sep(update, ",").map(_.foldRight(Hand(0, 0, 0))(_(_)))
|
||||||
|
|
||||||
val game = "Game" ~ num ~ ":" ~ repsep(hand, ";") ^^ {
|
val game = "Game" ~ num ~ ":" ~ repsep(hand, ";") ^^ {
|
||||||
|
|
Loading…
Reference in a new issue