From 214438d779633aa3b3c3c0bfbf07ee73a6de4f17 Mon Sep 17 00:00:00 2001 From: Soren Date: Mon, 2 Dec 2019 00:08:44 -0800 Subject: [PATCH] Update SBT, fix compilation issues, solve y2019 so far --- aoc/src/main/scala/aoc/Main.scala | 3 +- aoc/src/main/scala/aoc/y2018/Day04.scala | 38 ++++++------- aoc/src/main/scala/aoc/y2018/Day05.scala | 4 +- aoc/src/main/scala/aoc/y2019/Day01.scala | 22 ++++++++ aoc/src/main/scala/aoc/y2019/Day02.scala | 62 ++++++++++++++++++++++ aoc/src/main/scala/aoc/y2019/package.scala | 10 ++++ build.sbt | 21 ++++---- project/build.properties | 2 +- project/plugins.sbt | 8 +-- 9 files changed, 134 insertions(+), 36 deletions(-) create mode 100644 aoc/src/main/scala/aoc/y2019/Day01.scala create mode 100644 aoc/src/main/scala/aoc/y2019/Day02.scala create mode 100644 aoc/src/main/scala/aoc/y2019/package.scala diff --git a/aoc/src/main/scala/aoc/Main.scala b/aoc/src/main/scala/aoc/Main.scala index 2e0cc29..f8bb629 100644 --- a/aoc/src/main/scala/aoc/Main.scala +++ b/aoc/src/main/scala/aoc/Main.scala @@ -8,6 +8,7 @@ object Main { "2016" -> y2016.!, "2017" -> y2017.!, "2018" -> y2018.!, + "2019" -> y2019.!, ) def main(args: Array[String]): Unit = { @@ -15,7 +16,7 @@ object Main { val day = askForDay(year) val part = askForPart(day) println("Problem input? End input with an `End of Input` line.") - val input = Stream.continually(StdIn.readLine()).takeWhile(_ != "End of Input").toList.mkString("\n") + val input = LazyList.continually(StdIn.readLine()).takeWhile(_ != "End of Input").toList.mkString("\n") println(s"Output: ${part(input)}") } diff --git a/aoc/src/main/scala/aoc/y2018/Day04.scala b/aoc/src/main/scala/aoc/y2018/Day04.scala index f8ec09a..28df707 100644 --- a/aoc/src/main/scala/aoc/y2018/Day04.scala +++ b/aoc/src/main/scala/aoc/y2018/Day04.scala @@ -142,25 +142,25 @@ object Day04 extends Day { override def part1(input: String): String = { return ??? // Fixme: This all sucks and is broken please send an ambulance - val actions = getActions(input) - println(actions.mkString("\n")) - val dayGroups = actions.groupBy(_.t.d) - val fleshedOut: Map[Date, List[Option[Action]]] = dayGroups.mapValues(l => { - val zipped = l.map(_.t.t.mi).zip(l).toMap - List.tabulate(60 * 2)(n => zipped.get(n + (60 * 11))) - }) - val endState = fleshedOut.foldLeft(State()) { - case (s, (_, al)) => - al.foldLeft(s)(_.step(_)) - } - val days = endState.d - println() - val Day(_, Some(mostSleep), _) = days.maxBy(_.mins.count(!_.state)) - println(days.map(_.mins.size).mkString("\n")) - val maxMinutes = days.map(_.mins.size).max - val minutesToDays = days.map(_.mins.padTo(maxMinutes, Minute(true))).transpose - val (_, mostConsistency) = minutesToDays.zipWithIndex.maxBy { case (l, _) => l.count(!_.state) } - (mostSleep * mostConsistency).toString +// val actions = getActions(input) +// println(actions.mkString("\n")) +// val dayGroups = actions.groupBy(_.t.d) +// val fleshedOut: Map[Date, List[Option[Action]]] = dayGroups.mapValues(l => { +// val zipped = l.map(_.t.t.mi).zip(l).toMap +// List.tabulate(60 * 2)(n => zipped.get(n + (60 * 11))) +// }) +// val endState = fleshedOut.foldLeft(State()) { +// case (s, (_, al)) => +// al.foldLeft(s)(_.step(_)) +// } +// val days = endState.d +// println() +// val Day(_, Some(mostSleep), _) = days.maxBy(_.mins.count(!_.state)) +// println(days.map(_.mins.size).mkString("\n")) +// val maxMinutes = days.map(_.mins.size).max +// val minutesToDays = days.map(_.mins.padTo(maxMinutes, Minute(true))).transpose +// val (_, mostConsistency) = minutesToDays.zipWithIndex.maxBy { case (l, _) => l.count(!_.state) } +// (mostSleep * mostConsistency).toString } override def part2(input: String): String = ??? diff --git a/aoc/src/main/scala/aoc/y2018/Day05.scala b/aoc/src/main/scala/aoc/y2018/Day05.scala index bf7f05d..bc1cee3 100644 --- a/aoc/src/main/scala/aoc/y2018/Day05.scala +++ b/aoc/src/main/scala/aoc/y2018/Day05.scala @@ -32,8 +32,8 @@ object Day05 extends Day { } override def part2(input: String): String = { - val allChars = input.map(_.toLower).distinct.sortBy(c => input.count(_ == c)) - val mapped = allChars.par.map(c => simpleLength(input.filter(_.toLower != c))) + val allChars = input.map(_.toLower).toSeq.distinct.sortBy(c => input.count(_ == c)) + val mapped = allChars.map(c => simpleLength(input.filter(_.toLower != c))) mapped.min.toString } diff --git a/aoc/src/main/scala/aoc/y2019/Day01.scala b/aoc/src/main/scala/aoc/y2019/Day01.scala new file mode 100644 index 0000000..74ba324 --- /dev/null +++ b/aoc/src/main/scala/aoc/y2019/Day01.scala @@ -0,0 +1,22 @@ +package aoc.y2019 + +import aoc.Day + +object Day01 extends Day { + + def fuel(mass: Int): Int = (mass / 3) - 2 + + override def part1(input: String): String = { + val masses = input.linesIterator.map(_.toInt) + masses.map(fuel).sum[Int].toString + } + + override def part2(input: String): String = { + val masses = input.linesIterator.map(_.toInt) + masses.map { initial => + lazy val fuels: LazyList[Int] = fuel(initial) #:: fuels.map(fuel) + fuels.takeWhile(_ > 0).sum + }.sum[Int].toString + } + +} diff --git a/aoc/src/main/scala/aoc/y2019/Day02.scala b/aoc/src/main/scala/aoc/y2019/Day02.scala new file mode 100644 index 0000000..2967ea6 --- /dev/null +++ b/aoc/src/main/scala/aoc/y2019/Day02.scala @@ -0,0 +1,62 @@ +package aoc.y2019 + +import aoc.Day +import cats.data.State + +object Day02 extends Day { + + case class Machine(memory: List[Int], pointer: Int) + type MachineState = State[Machine, Boolean] + + val ops: Map[Int, (Int, Int) => Int] = Map( + 1 -> (_ + _), + 2 -> (_ * _) + ) + + def initialReplacement(noun: Int, verb: Int): MachineState = State { + case Machine(memory, 0) => + (Machine(memory.updated(1, noun).updated(2, verb), 0), false) + } + + val step: MachineState = State { + case Machine(memory, pointer) => + memory(pointer) match { + case 99 => (Machine(memory, pointer), true) + case opcode => + val inputAAddress = memory(pointer + 1) + val inputBAddress = memory(pointer + 2) + val valueA = memory(inputAAddress) + val valueB = memory(inputBAddress) + val outputAddress = memory(pointer + 3) + val op = ops(opcode) + (Machine(memory.updated(outputAddress, op(valueA, valueB)), pointer + 4), false) + } + } + + def runMachine(memory: List[Int], noun: Int, verb: Int): Int = { + val initialState = Machine(memory, 0) + val initialReplacementState = initialReplacement(noun, verb).run(initialState).value + lazy val states: LazyList[(Machine, Boolean)] = + initialReplacementState #:: states.map { + case (currentState, _) => step.run(currentState).value + } + val (lastState, _) = states.takeWhile(!_._2).last + lastState.memory.head + } + + override def part1(input: String): String = { + val memory = input.split(",").map(_.toInt).toList + runMachine(memory, 12, 2).toString + } + + override def part2(input: String): String = { + val memory = input.split(",").map(_.toInt).toList + val magic = 19690720 + val solutions = for { + noun <- 0 to 99 + verb <- 0 to 99 if runMachine(memory, noun, verb) == magic + } yield noun * 100 + verb + solutions.head.toString + } + +} diff --git a/aoc/src/main/scala/aoc/y2019/package.scala b/aoc/src/main/scala/aoc/y2019/package.scala new file mode 100644 index 0000000..f61d5c7 --- /dev/null +++ b/aoc/src/main/scala/aoc/y2019/package.scala @@ -0,0 +1,10 @@ +package aoc + +package object y2019 extends Year { + + override def days: Map[String, Day] = Map( + "1" -> Day01, + "2" -> Day02, + ) + +} diff --git a/build.sbt b/build.sbt index d7df41b..3cd54ca 100644 --- a/build.sbt +++ b/build.sbt @@ -4,18 +4,21 @@ val sharedSettings = Seq( name := "aoc", organization := "tf.bug", version := "0.1.0", - scalaVersion := "2.12.7", + scalaVersion := "2.13.1", libraryDependencies ++= List( - "com.lihaoyi" %%% "fastparse" % "2.0.4", - "org.scala-graph" %%% "graph-core" % "1.12.5", + "com.lihaoyi" %%% "fastparse" % "2.1.3", + "org.scala-graph" %%% "graph-core" % "1.13.1", + "org.typelevel" %%% "cats-core" % "2.0.0", ), mainClass := Some("tf.bug.aoc.Main"), ) -lazy val aoc = crossProject(JSPlatform, JVMPlatform /* , NativePlatform */ ) - .crossType(CrossType.Pure) - .settings(sharedSettings) - -lazy val aocJS = aoc.js -lazy val aocJVM = aoc.jvm +// lazy val aoc = crossProject(/* JSPlatform, */ JVMPlatform /* , NativePlatform */ ) +// .crossType(CrossType.Pure) +// .settings(sharedSettings) +// +// lazy val aocJS = aoc.js +// lazy val aocJVM = aoc.jvm // lazy val aocNative = aoc.native + +lazy val aoc = (project in file("aoc")).settings(sharedSettings) diff --git a/project/build.properties b/project/build.properties index e71780d..b7382c8 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version = 1.2.6 \ No newline at end of file +sbt.version = 1.3.1 \ No newline at end of file diff --git a/project/plugins.sbt b/project/plugins.sbt index c9679ad..ff2bc96 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,4 +1,4 @@ -addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "0.6.0") -addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "0.6.0") -addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.25") -addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.3.8") +addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "0.6.1") +addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "0.6.1") +addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.31") +addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.3.9")