Format
This commit is contained in:
parent
cf0b23d5a5
commit
b0222f3f42
3 changed files with 20 additions and 10 deletions
|
@ -40,9 +40,13 @@ object Day03 extends Day {
|
||||||
}
|
}
|
||||||
def sumOfSurrounding(p: (Int, Int), m: Map[(Int, Int), Int]): Int = {
|
def sumOfSurrounding(p: (Int, Int), m: Map[(Int, Int), Int]): Int = {
|
||||||
val surrounding = List(
|
val surrounding = List(
|
||||||
(-1, 1), ( 0, 1), ( 1, 1),
|
(-1, 1),
|
||||||
|
(0, 1),
|
||||||
|
(1, 1),
|
||||||
(-1, 0), /* */ (1, 0),
|
(-1, 0), /* */ (1, 0),
|
||||||
(-1, -1), ( 0, -1), ( 1, -1)
|
(-1, -1),
|
||||||
|
(0, -1),
|
||||||
|
(1, -1)
|
||||||
)
|
)
|
||||||
surrounding.map(o => m.getOrElse(p + o, 0)).sum
|
surrounding.map(o => m.getOrElse(p + o, 0)).sum
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,8 @@ object Day07 extends Day {
|
||||||
def childrenParser[_: P]: P[List[String]] =
|
def childrenParser[_: P]: P[List[String]] =
|
||||||
P(("->" ~ CharIn("a-z").rep(1).!.rep(1, ",")).?).map(_.fold(List[String]())(_.toList))
|
P(("->" ~ CharIn("a-z").rep(1).!.rep(1, ",")).?).map(_.fold(List[String]())(_.toList))
|
||||||
|
|
||||||
def towerParser[_: P]: P[TowerRef] = P(CharIn("a-z").rep(1).! ~ "(" ~ CharIn("0-9").rep(1).! ~ ")" ~ childrenParser).map {
|
def towerParser[_: P]: P[TowerRef] =
|
||||||
|
P(CharIn("a-z").rep(1).! ~ "(" ~ CharIn("0-9").rep(1).! ~ ")" ~ childrenParser).map {
|
||||||
case (name, weight, children) => TowerRef(name, weight.toInt, children)
|
case (name, weight, children) => TowerRef(name, weight.toInt, children)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,10 +44,12 @@ object Day08 extends Day {
|
||||||
|
|
||||||
def registerParser[_: P] = P(CharIn("a-z").rep(1).!)
|
def registerParser[_: P] = P(CharIn("a-z").rep(1).!)
|
||||||
def numberParser[_: P] = P(("-".? ~ CharIn("0-9").rep(1)).!).map(_.toInt)
|
def numberParser[_: P] = P(("-".? ~ CharIn("0-9").rep(1)).!).map(_.toInt)
|
||||||
|
|
||||||
def instructionParser[_: P] = P(("inc" | "dec").!).map {
|
def instructionParser[_: P] = P(("inc" | "dec").!).map {
|
||||||
case "inc" => Inc
|
case "inc" => Inc
|
||||||
case "dec" => Dec
|
case "dec" => Dec
|
||||||
}
|
}
|
||||||
|
|
||||||
def testParser[_: P] = P(("<=" | ">=" | "<" | ">" | "==" | "!=").!).map {
|
def testParser[_: P] = P(("<=" | ">=" | "<" | ">" | "==" | "!=").!).map {
|
||||||
case "<" => Lt
|
case "<" => Lt
|
||||||
case ">" => Gt
|
case ">" => Gt
|
||||||
|
@ -56,12 +58,15 @@ object Day08 extends Day {
|
||||||
case "==" => Eq
|
case "==" => Eq
|
||||||
case "!=" => Neq
|
case "!=" => Neq
|
||||||
}
|
}
|
||||||
|
|
||||||
def fullInstructionParser[_: P] = P(registerParser ~ " " ~ instructionParser ~ " " ~ numberParser).map {
|
def fullInstructionParser[_: P] = P(registerParser ~ " " ~ instructionParser ~ " " ~ numberParser).map {
|
||||||
case (reg, op, v) => FullInstruction(reg, op, v)
|
case (reg, op, v) => FullInstruction(reg, op, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
def fullTestParser[_: P] = P(registerParser ~ " " ~ testParser ~ " " ~ numberParser).map {
|
def fullTestParser[_: P] = P(registerParser ~ " " ~ testParser ~ " " ~ numberParser).map {
|
||||||
case (reg, op, v) => FullTest(reg, op, v)
|
case (reg, op, v) => FullTest(reg, op, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
def lineParser[_: P] = P(fullInstructionParser ~ " if " ~ fullTestParser).map {
|
def lineParser[_: P] = P(fullInstructionParser ~ " if " ~ fullTestParser).map {
|
||||||
case (i, t) => Line(i, t)
|
case (i, t) => Line(i, t)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue