Update SBT, fix compilation issues, solve y2019 so far

This commit is contained in:
Aly 2019-12-02 00:08:44 -08:00 committed by Anthony Cerruti
parent 4ae0f0c1b3
commit 214438d779
9 changed files with 134 additions and 36 deletions

View File

@ -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)}")
}

View File

@ -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 = ???

View File

@ -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
}

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -0,0 +1,10 @@
package aoc
package object y2019 extends Year {
override def days: Map[String, Day] = Map(
"1" -> Day01,
"2" -> Day02,
)
}

View File

@ -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)

View File

@ -1 +1 @@
sbt.version = 1.2.6
sbt.version = 1.3.1

View File

@ -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")