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 = {
|
||||
val surrounding = List(
|
||||
(-1, 1), ( 0, 1), ( 1, 1),
|
||||
(-1, 0), /* */ ( 1, 0),
|
||||
(-1, -1), ( 0, -1), ( 1, -1)
|
||||
(-1, 1),
|
||||
(0, 1),
|
||||
(1, 1),
|
||||
(-1, 0), /* */ (1, 0),
|
||||
(-1, -1),
|
||||
(0, -1),
|
||||
(1, -1)
|
||||
)
|
||||
surrounding.map(o => m.getOrElse(p + o, 0)).sum
|
||||
}
|
||||
|
@ -58,7 +62,7 @@ object Day03 extends Day {
|
|||
val np = p + nd.change
|
||||
val sum = sumOfSurrounding(np, m)
|
||||
val nm = m + (np -> sum)
|
||||
if(sum > targetNum) {
|
||||
if (sum > targetNum) {
|
||||
sum
|
||||
} else {
|
||||
findNext(nm, np, i)
|
||||
|
|
|
@ -24,9 +24,10 @@ object Day07 extends Day {
|
|||
def childrenParser[_: P]: P[List[String]] =
|
||||
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 {
|
||||
case (name, weight, children) => TowerRef(name, weight.toInt, children)
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
def towerListParser[_: P]: P[List[TowerRef]] = towerParser.rep(1, "\n").map(_.toList)
|
||||
|
||||
|
@ -46,10 +47,10 @@ object Day07 extends Day {
|
|||
val lowestT = lowest(trs)
|
||||
val resolved = lowestT.resolve(trs)
|
||||
def searchForUnevenness(tower: Tower): List[Int] = {
|
||||
if(tower.childWeights.distinct.size == 1) {
|
||||
if (tower.childWeights.distinct.size == 1) {
|
||||
tower.children.flatMap(searchForUnevenness)
|
||||
} else {
|
||||
if(tower.childWeights.isEmpty) {
|
||||
if (tower.childWeights.isEmpty) {
|
||||
List()
|
||||
} else {
|
||||
val ds = tower.childWeights.distinct
|
||||
|
|
|
@ -44,10 +44,12 @@ object Day08 extends Day {
|
|||
|
||||
def registerParser[_: P] = P(CharIn("a-z").rep(1).!)
|
||||
def numberParser[_: P] = P(("-".? ~ CharIn("0-9").rep(1)).!).map(_.toInt)
|
||||
|
||||
def instructionParser[_: P] = P(("inc" | "dec").!).map {
|
||||
case "inc" => Inc
|
||||
case "dec" => Dec
|
||||
}
|
||||
|
||||
def testParser[_: P] = P(("<=" | ">=" | "<" | ">" | "==" | "!=").!).map {
|
||||
case "<" => Lt
|
||||
case ">" => Gt
|
||||
|
@ -56,12 +58,15 @@ object Day08 extends Day {
|
|||
case "==" => Eq
|
||||
case "!=" => Neq
|
||||
}
|
||||
|
||||
def fullInstructionParser[_: P] = P(registerParser ~ " " ~ instructionParser ~ " " ~ numberParser).map {
|
||||
case (reg, op, v) => FullInstruction(reg, op, v)
|
||||
}
|
||||
|
||||
def fullTestParser[_: P] = P(registerParser ~ " " ~ testParser ~ " " ~ numberParser).map {
|
||||
case (reg, op, v) => FullTest(reg, op, v)
|
||||
}
|
||||
|
||||
def lineParser[_: P] = P(fullInstructionParser ~ " if " ~ fullTestParser).map {
|
||||
case (i, t) => Line(i, t)
|
||||
}
|
||||
|
@ -81,7 +86,7 @@ object Day08 extends Day {
|
|||
val testReg = map(cl.t.reg)
|
||||
val test = cl.t.op
|
||||
val v = cl.t.v
|
||||
if(test.result(testReg, v)) {
|
||||
if (test.result(testReg, v)) {
|
||||
val inReg = map(cl.i.reg)
|
||||
val in = cl.i.op
|
||||
val iv = cl.i.v
|
||||
|
|
Loading…
Reference in a new issue