Format
This commit is contained in:
parent
21e7449350
commit
02d49f7dd6
2 changed files with 53 additions and 39 deletions
|
@ -45,8 +45,10 @@ object AoC2017 extends Year {
|
||||||
val lines = input.lines.toList
|
val lines = input.lines.toList
|
||||||
val rows = lines.map(_.split("""\s+""").map(_.toInt))
|
val rows = lines.map(_.split("""\s+""").map(_.toInt))
|
||||||
rows.flatMap { r =>
|
rows.flatMap { r =>
|
||||||
val q = r.zipWithIndex.flatMap { case (e, i) =>
|
val q = r.zipWithIndex.flatMap {
|
||||||
r.drop(i + 1).map(f => {
|
case (e, i) =>
|
||||||
|
r.drop(i + 1)
|
||||||
|
.map(f => {
|
||||||
val (min, max) = (Math.min(e, f), Math.max(e, f))
|
val (min, max) = (Math.min(e, f), Math.max(e, f))
|
||||||
if (max % min == 0) {
|
if (max % min == 0) {
|
||||||
Some(max / min)
|
Some(max / min)
|
||||||
|
@ -99,7 +101,13 @@ object AoC2017 extends Year {
|
||||||
case object MoveRight extends Direction {
|
case object MoveRight extends Direction {
|
||||||
override def change: Int = 1
|
override def change: Int = 1
|
||||||
}
|
}
|
||||||
case class Program(beginIn: State, diagnosticCount: Int, states: Map[State, InState], index: Int = 0, tape: Map[Int, Int] = Map()) {
|
case class Program(
|
||||||
|
beginIn: State,
|
||||||
|
diagnosticCount: Int,
|
||||||
|
states: Map[State, InState],
|
||||||
|
index: Int = 0,
|
||||||
|
tape: Map[Int, Int] = Map()
|
||||||
|
) {
|
||||||
|
|
||||||
def step: Program = {
|
def step: Program = {
|
||||||
val ndc = diagnosticCount - 1
|
val ndc = diagnosticCount - 1
|
||||||
|
@ -120,20 +128,26 @@ object AoC2017 extends Year {
|
||||||
|
|
||||||
def stateParser[_: P]: P[State] = P("state" ~ CharIn("A-Z").rep.!).map(State)
|
def stateParser[_: P]: P[State] = P("state" ~ CharIn("A-Z").rep.!).map(State)
|
||||||
def writeParser[_: P]: P[Int] = P("- Write the value" ~ ("0" | "1").! ~ ".").map(_.toInt)
|
def writeParser[_: P]: P[Int] = P("- Write the value" ~ ("0" | "1").! ~ ".").map(_.toInt)
|
||||||
|
|
||||||
def directionParser[_: P]: P[Direction] = P("- Move one slot to the" ~ ("left" | "right").! ~ ".").map {
|
def directionParser[_: P]: P[Direction] = P("- Move one slot to the" ~ ("left" | "right").! ~ ".").map {
|
||||||
case "left" => MoveLeft
|
case "left" => MoveLeft
|
||||||
case "right" => MoveRight
|
case "right" => MoveRight
|
||||||
}
|
}
|
||||||
def stateChangeParser[_: P]: P[State] = P("- Continue with" ~ stateParser ~ ".")
|
def stateChangeParser[_: P]: P[State] = P("- Continue with" ~ stateParser ~ ".")
|
||||||
|
|
||||||
def branchParser[_: P]: P[Branch] = P(writeParser ~ directionParser ~ stateChangeParser).map {
|
def branchParser[_: P]: P[Branch] = P(writeParser ~ directionParser ~ stateChangeParser).map {
|
||||||
case (write, dir, state) => Branch(write, dir, state)
|
case (write, dir, state) => Branch(write, dir, state)
|
||||||
}
|
}
|
||||||
def inStateParser[_: P]: P[InState] = P(
|
|
||||||
|
def inStateParser[_: P]: P[InState] =
|
||||||
|
P(
|
||||||
"In" ~ stateParser ~ ":" ~ "If the current value is 0:" ~ branchParser ~ "If the current value is 1:" ~ branchParser
|
"In" ~ stateParser ~ ":" ~ "If the current value is 0:" ~ branchParser ~ "If the current value is 1:" ~ branchParser
|
||||||
).map {
|
).map {
|
||||||
case (state, v0, v1) => InState(state, v0, v1)
|
case (state, v0, v1) => InState(state, v0, v1)
|
||||||
}
|
}
|
||||||
def programParser[_: P]: P[Program] = P(
|
|
||||||
|
def programParser[_: P]: P[Program] =
|
||||||
|
P(
|
||||||
"Begin in" ~ stateParser ~ "." ~
|
"Begin in" ~ stateParser ~ "." ~
|
||||||
"Perform a diagnostic checksum after" ~ CharIn("0-9").rep(1).!.map(_.toInt) ~ "steps." ~
|
"Perform a diagnostic checksum after" ~ CharIn("0-9").rep(1).!.map(_.toInt) ~ "steps." ~
|
||||||
inStateParser.rep.map(f => {
|
inStateParser.rep.map(f => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue