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),
|
||||||
(-1, 0), /* */ ( 1, 0),
|
(0, 1),
|
||||||
(-1, -1), ( 0, -1), ( 1, -1)
|
(1, 1),
|
||||||
|
(-1, 0), /* */ (1, 0),
|
||||||
|
(-1, -1),
|
||||||
|
(0, -1),
|
||||||
|
(1, -1)
|
||||||
)
|
)
|
||||||
surrounding.map(o => m.getOrElse(p + o, 0)).sum
|
surrounding.map(o => m.getOrElse(p + o, 0)).sum
|
||||||
}
|
}
|
||||||
|
@ -58,7 +62,7 @@ object Day03 extends Day {
|
||||||
val np = p + nd.change
|
val np = p + nd.change
|
||||||
val sum = sumOfSurrounding(np, m)
|
val sum = sumOfSurrounding(np, m)
|
||||||
val nm = m + (np -> sum)
|
val nm = m + (np -> sum)
|
||||||
if(sum > targetNum) {
|
if (sum > targetNum) {
|
||||||
sum
|
sum
|
||||||
} else {
|
} else {
|
||||||
findNext(nm, np, i)
|
findNext(nm, np, i)
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,10 +47,10 @@ object Day07 extends Day {
|
||||||
val lowestT = lowest(trs)
|
val lowestT = lowest(trs)
|
||||||
val resolved = lowestT.resolve(trs)
|
val resolved = lowestT.resolve(trs)
|
||||||
def searchForUnevenness(tower: Tower): List[Int] = {
|
def searchForUnevenness(tower: Tower): List[Int] = {
|
||||||
if(tower.childWeights.distinct.size == 1) {
|
if (tower.childWeights.distinct.size == 1) {
|
||||||
tower.children.flatMap(searchForUnevenness)
|
tower.children.flatMap(searchForUnevenness)
|
||||||
} else {
|
} else {
|
||||||
if(tower.childWeights.isEmpty) {
|
if (tower.childWeights.isEmpty) {
|
||||||
List()
|
List()
|
||||||
} else {
|
} else {
|
||||||
val ds = tower.childWeights.distinct
|
val ds = tower.childWeights.distinct
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -81,7 +86,7 @@ object Day08 extends Day {
|
||||||
val testReg = map(cl.t.reg)
|
val testReg = map(cl.t.reg)
|
||||||
val test = cl.t.op
|
val test = cl.t.op
|
||||||
val v = cl.t.v
|
val v = cl.t.v
|
||||||
if(test.result(testReg, v)) {
|
if (test.result(testReg, v)) {
|
||||||
val inReg = map(cl.i.reg)
|
val inReg = map(cl.i.reg)
|
||||||
val in = cl.i.op
|
val in = cl.i.op
|
||||||
val iv = cl.i.v
|
val iv = cl.i.v
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue