This commit is contained in:
Aly 2018-11-24 16:30:29 -08:00
parent 21e7449350
commit 02d49f7dd6
No known key found for this signature in database
GPG key ID: 555B7346639DDAC3
2 changed files with 53 additions and 39 deletions

View file

@ -15,7 +15,7 @@ object AoC2017 extends Year {
val reg = """(\d)(?=\1)""".r val reg = """(\d)(?=\1)""".r
val matches = reg.findAllIn(input).toList val matches = reg.findAllIn(input).toList
val csum = matches.map(_.toInt).sum val csum = matches.map(_.toInt).sum
if(input.head == input.last) { if (input.head == input.last) {
(csum + input.head.toString.toInt).toString (csum + input.head.toString.toInt).toString
} else { } else {
csum.toString csum.toString
@ -25,7 +25,7 @@ object AoC2017 extends Year {
override def part2(input: String): String = { override def part2(input: String): String = {
val (left, right) = input.splitAt(input.length / 2) val (left, right) = input.splitAt(input.length / 2)
val zipped = left.zip(right) val zipped = left.zip(right)
val doubled = zipped.filter { case (l, r) => l == r}.map { case (c, _) => c.toString } val doubled = zipped.filter { case (l, r) => l == r }.map { case (c, _) => c.toString }
(doubled.map(_.toInt).sum * 2).toString (doubled.map(_.toInt).sum * 2).toString
} }
@ -45,15 +45,17 @@ 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) =>
val (min, max) = (Math.min(e, f), Math.max(e, f)) r.drop(i + 1)
if(max % min == 0) { .map(f => {
Some(max / min) val (min, max) = (Math.min(e, f), Math.max(e, f))
} else { if (max % min == 0) {
None Some(max / min)
} } else {
}) None
}
})
} }
q.filter(_.isDefined).map(_.get) q.filter(_.isDefined).map(_.get)
}.sum.toString }.sum.toString
@ -67,7 +69,7 @@ object AoC2017 extends Year {
case class Cell(num: Int, stepSize: Int, stepFrac: Int, stepped: Int, dist: (Int, Int)) case class Cell(num: Int, stepSize: Int, stepFrac: Int, stepped: Int, dist: (Int, Int))
val inum = input.toInt val inum = input.toInt
(inum match { (inum match {
case 1 => 0 case 1 => 0
case _ => case _ =>
val layer = (Math.sqrt(inum).ceil / 2).floor.toInt val layer = (Math.sqrt(inum).ceil / 2).floor.toInt
val square = ((layer + 1) * 2) - 1 val square = ((layer + 1) * 2) - 1
@ -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
@ -118,31 +126,37 @@ 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(
"In" ~ stateParser ~ ":" ~ "If the current value is 0:" ~ branchParser ~ "If the current value is 1:" ~ branchParser def inStateParser[_: P]: P[InState] =
).map { P(
case (state, v0, v1) => InState(state, v0, v1) "In" ~ stateParser ~ ":" ~ "If the current value is 0:" ~ branchParser ~ "If the current value is 1:" ~ branchParser
} ).map {
def programParser[_: P]: P[Program] = P( case (state, v0, v1) => InState(state, v0, v1)
"Begin in" ~ stateParser ~ "." ~ }
"Perform a diagnostic checksum after" ~ CharIn("0-9").rep(1).!.map(_.toInt) ~ "steps." ~
inStateParser.rep.map(f => { def programParser[_: P]: P[Program] =
val z = f.map(_.state).zip(f) P(
z.toMap "Begin in" ~ stateParser ~ "." ~
}) "Perform a diagnostic checksum after" ~ CharIn("0-9").rep(1).!.map(_.toInt) ~ "steps." ~
).map { inStateParser.rep.map(f => {
case (begin, steps, states) => Program(begin, steps, states) val z = f.map(_.state).zip(f)
} z.toMap
})
).map {
case (begin, steps, states) => Program(begin, steps, states)
}
override def part1(input: String): String = { override def part1(input: String): String = {
val Parsed.Success(program, _) = parse(input, programParser(_)) val Parsed.Success(program, _) = parse(input, programParser(_))

View file

@ -1,9 +1,9 @@
import sbtcrossproject.CrossPlugin.autoImport.{crossProject, CrossType} import sbtcrossproject.CrossPlugin.autoImport.{crossProject, CrossType}
val sharedSettings = Seq( val sharedSettings = Seq(
name := "aoc", name := "aoc",
organization := "tf.bug", organization := "tf.bug",
version := "0.1.0", version := "0.1.0",
scalaVersion := "2.11.12", scalaVersion := "2.11.12",
libraryDependencies ++= List( libraryDependencies ++= List(
"com.lihaoyi" %%% "fastparse" % "2.0.4", "com.lihaoyi" %%% "fastparse" % "2.0.4",
@ -11,10 +11,10 @@ val sharedSettings = Seq(
mainClass := Some("tf.bug.aoc.Main"), mainClass := Some("tf.bug.aoc.Main"),
) )
lazy val aoc = crossProject(JSPlatform, JVMPlatform /* , NativePlatform */) lazy val aoc = crossProject(JSPlatform, JVMPlatform /* , NativePlatform */ )
.crossType(CrossType.Pure) .crossType(CrossType.Pure)
.settings(sharedSettings) .settings(sharedSettings)
lazy val aocJS = aoc.js lazy val aocJS = aoc.js
lazy val aocJVM = aoc.jvm lazy val aocJVM = aoc.jvm
// lazy val aocNative = aoc.native // lazy val aocNative = aoc.native