38 lines
1004 B
Scala
38 lines
1004 B
Scala
package aoc.y2019
|
|
|
|
import aoc.Day
|
|
import cats._
|
|
import cats.implicits._
|
|
import cats.data.State
|
|
|
|
object Day02 extends Day {
|
|
|
|
def initialReplacement(noun: Int, verb: Int): Intcode.Operation = Intcode.set(1, noun) *> Intcode.set(2, verb)
|
|
|
|
def runMachine(memory: Vector[Int], noun: Int, verb: Int): Int = {
|
|
val initialState = Intcode.Machine(memory)
|
|
val runMachine = for {
|
|
_ <- initialReplacement(noun, verb)
|
|
ran <- Intcode.run()
|
|
} yield ran
|
|
val endState = runMachine.runS(initialState).value
|
|
endState.memory(0)
|
|
}
|
|
|
|
override def part1(input: String): String = {
|
|
val memory = input.split(",").map(_.toInt).toVector
|
|
runMachine(memory, 12, 2).toString
|
|
}
|
|
|
|
override def part2(input: String): String = {
|
|
val memory = input.split(",").map(_.toInt).toVector
|
|
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
|
|
}
|
|
|
|
}
|