2019-12-02 08:08:44 +00:00
|
|
|
package aoc.y2019
|
|
|
|
|
|
|
|
import aoc.Day
|
2019-12-09 07:58:14 +00:00
|
|
|
import aoc.y2019.intcode.{Machine, Step}
|
2019-12-07 22:42:54 +00:00
|
|
|
import cats._
|
|
|
|
import cats.implicits._
|
2019-12-02 08:08:44 +00:00
|
|
|
import cats.data.State
|
|
|
|
|
|
|
|
object Day02 extends Day {
|
|
|
|
|
2019-12-09 07:58:14 +00:00
|
|
|
def initialReplacement(noun: Int, verb: Int): Step = Machine.set(1, noun) *> Machine.set(2, verb)
|
2019-12-02 08:08:44 +00:00
|
|
|
|
2019-12-09 07:58:14 +00:00
|
|
|
def runMachine(memory: Vector[BigInt], noun: Int, verb: Int): BigInt = {
|
|
|
|
val initialState = Machine(memory)
|
2019-12-05 06:15:10 +00:00
|
|
|
val runMachine = for {
|
|
|
|
_ <- initialReplacement(noun, verb)
|
2019-12-09 07:58:14 +00:00
|
|
|
ran <- Machine.run()
|
2019-12-07 22:42:54 +00:00
|
|
|
} yield ran
|
2019-12-05 06:15:10 +00:00
|
|
|
val endState = runMachine.runS(initialState).value
|
|
|
|
endState.memory(0)
|
2019-12-02 08:08:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override def part1(input: String): String = {
|
2019-12-09 07:58:14 +00:00
|
|
|
val memory = input.split(",").map(BigInt(_)).toVector
|
2019-12-02 08:08:44 +00:00
|
|
|
runMachine(memory, 12, 2).toString
|
|
|
|
}
|
|
|
|
|
|
|
|
override def part2(input: String): String = {
|
2019-12-09 07:58:14 +00:00
|
|
|
val memory = input.split(",").map(BigInt(_)).toVector
|
2019-12-02 08:08:44 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|