Commit c775c9d2 authored by David's avatar David

Advanced IntCode engine!

parent f73eac46
3,225,1,225,6,6,1100,1,238,225,104,0,2,136,183,224,101,-5304,224,224,4,224,1002,223,8,223,1001,224,6,224,1,224,223,223,1101,72,47,225,1101,59,55,225,1101,46,75,225,1101,49,15,224,101,-64,224,224,4,224,1002,223,8,223,1001,224,5,224,1,224,223,223,102,9,210,224,1001,224,-270,224,4,224,1002,223,8,223,1001,224,2,224,1,223,224,223,101,14,35,224,101,-86,224,224,4,224,1002,223,8,223,101,4,224,224,1,224,223,223,1102,40,74,224,1001,224,-2960,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,1101,10,78,225,1001,39,90,224,1001,224,-149,224,4,224,102,8,223,223,1001,224,4,224,1,223,224,223,1002,217,50,224,1001,224,-1650,224,4,224,1002,223,8,223,1001,224,7,224,1,224,223,223,1102,68,8,225,1,43,214,224,1001,224,-126,224,4,224,102,8,223,223,101,3,224,224,1,224,223,223,1102,88,30,225,1102,18,80,225,1102,33,28,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,108,677,677,224,102,2,223,223,1005,224,329,1001,223,1,223,1107,677,226,224,102,2,223,223,1006,224,344,1001,223,1,223,108,226,226,224,102,2,223,223,1005,224,359,1001,223,1,223,1108,677,226,224,102,2,223,223,1006,224,374,101,1,223,223,108,677,226,224,102,2,223,223,1006,224,389,1001,223,1,223,107,226,226,224,102,2,223,223,1005,224,404,1001,223,1,223,8,226,226,224,102,2,223,223,1006,224,419,101,1,223,223,1107,677,677,224,102,2,223,223,1006,224,434,1001,223,1,223,1107,226,677,224,1002,223,2,223,1006,224,449,101,1,223,223,7,677,677,224,1002,223,2,223,1006,224,464,1001,223,1,223,1108,226,677,224,1002,223,2,223,1005,224,479,1001,223,1,223,8,677,226,224,1002,223,2,223,1005,224,494,101,1,223,223,7,226,677,224,102,2,223,223,1005,224,509,101,1,223,223,1008,677,226,224,102,2,223,223,1006,224,524,101,1,223,223,8,226,677,224,1002,223,2,223,1006,224,539,1001,223,1,223,1007,677,677,224,102,2,223,223,1005,224,554,101,1,223,223,107,226,677,224,1002,223,2,223,1005,224,569,1001,223,1,223,1108,677,677,224,1002,223,2,223,1006,224,584,1001,223,1,223,1008,226,226,224,1002,223,2,223,1005,224,599,101,1,223,223,1008,677,677,224,102,2,223,223,1005,224,614,101,1,223,223,7,677,226,224,1002,223,2,223,1005,224,629,1001,223,1,223,107,677,677,224,1002,223,2,223,1006,224,644,101,1,223,223,1007,226,677,224,1002,223,2,223,1005,224,659,1001,223,1,223,1007,226,226,224,102,2,223,223,1005,224,674,101,1,223,223,4,223,99,226
package is.kow.adventofcode._2019
import java.util
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
abstract class OpCode(val opValue: Int, val parameterSize: Int) {
val opCodeFormatted: String = codeFormat(opValue)
protected final val IMMEDIATE = '1'
protected final val POSITION = '0'
def codeFormat(input: Int): String = {
("%0" + (parameterSize + 2) + "d").format(input)
}
def operate(offset: Int, data: List[Int]): List[Int]
def matches(code: Int): Boolean = {
//convert the code to a string also
val actualOpCode = codeFormat(code).slice(parameterSize, parameterSize + 2)
//Determine if the last two of the formatted thing, as an integer, are our opcode
opCodeFormatted.slice(parameterSize, parameterSize + 2) == actualOpCode
}
}
class AddOpCode() extends OpCode(1, 3) {
override def operate(offset: Int, data: List[Int]): List[Int] = {
data.drop(offset) match {
case opParams :: in1 :: in2 :: dest :: xs =>
val formattedOpParams = codeFormat(opParams)
println(s"OpcodeFormatted: ${formattedOpParams}")
val operand1 = if (formattedOpParams(2) == POSITION) data(in1) else in1
val operand2 = if (formattedOpParams(1) == POSITION) data(in2) else in2
data.updated(dest, operand1 + operand2)
}
}
}
class MultOpCode() extends OpCode(2, 3) {
override def operate(offset: Int, data: List[Int]): List[Int] = {
data.drop(offset) match {
case opParams :: in1 :: in2 :: dest :: xs =>
val formattedOpParams = codeFormat(opParams)
val operand1 = if (formattedOpParams(2) == POSITION) data(in1) else in1
val operand2 = if (formattedOpParams(1) == POSITION) data(in2) else in2
data.updated(dest, operand1 * operand2)
}
}
}
class InputOpCode(val input: Int) extends OpCode(3, 1) {
override def operate(offset: Int, data: List[Int]): List[Int] = {
data.drop(offset) match {
case opParams :: p1 :: xs =>
data.updated(p1, input)
}
}
}
//Can have multiple outputs...
class OutputOpCode(output: (Int => Unit)) extends OpCode(4, 1) {
override def operate(offset: Int, data: List[Int]): List[Int] = {
data.drop(offset) match {
case opParams :: p1 :: xs =>
println(s"OUTPUT: ${data(p1)}")
output(data(p1))
data
}
}
}
case class EndOpCode() extends OpCode(99, 0) {
override def operate(offset: Int, data: List[Int]): List[Int] = {
data
}
}
class IntCode(data: List[Int]) {
//A mutable state in here to collect the outputs in order
val outputCollector: mutable.Stack[Int] = new mutable.Stack[Int]()
private val outputOpCode = new OutputOpCode({ output =>
outputCollector.push(output)
})
def execute(input: Option[Int]): List[Int] = {
val END_OP = EndOpCode()
val OP_CODES = List(new AddOpCode(), new MultOpCode(), END_OP, outputOpCode) ++ (if (input.isDefined) List(new InputOpCode(input.get)) else List.empty)
@tailrec def process(completedOps: List[OpCode], acc: List[Int], opLocation: Int = 0): List[Int] = {
println(s"processing at ${opLocation}")
val currentOp = acc.drop(opLocation).head
if (END_OP.matches(currentOp)) {
acc
} else {
val maybeOp = OP_CODES.find(_.matches(currentOp))
val operated = maybeOp.map { operation =>
operation.operate(opLocation, acc)
} getOrElse {
throw new RuntimeException(s"Invalid Operation: $currentOp")
}
val op = maybeOp.get
process(op :: completedOps, operated, opLocation + 1 + op.parameterSize)
}
}
process(List.empty, data)
}
}
package is.kow.adventofcode._2019
import scala.io.Source
object Puzzle5 extends App {
val source = Source.fromResource("input.txt")
try {
val inputLine = source.getLines().toList.head
val ops = inputLine.split(",").map(_.toInt).toList
val intCodeEngine = new IntCode(ops)
val result = intCodeEngine.execute(Option(1))
val output = intCodeEngine.outputCollector
println(s"output: ${output.mkString(",")}")
} finally {
source.close()
}
}
package is.kow.adventofcode._2019
import org.junit.runner.RunWith
import org.scalatest.funspec.AnyFunSpec
import org.scalatestplus.junit.JUnitRunner
@RunWith(classOf[JUnitRunner])
class Puzzle5Spec extends AnyFunSpec {
describe("Old Opcode spec") {
Map(
List(1, 0, 0, 0, 99) -> List(2, 0, 0, 0, 99),
List(2, 3, 0, 3, 99) -> List(2, 3, 0, 6, 99),
List(2, 4, 4, 5, 99, 0) -> List(2, 4, 4, 5, 99, 9801),
List(1, 1, 1, 4, 99, 5, 6, 0, 99) -> List(30, 1, 1, 4, 2, 5, 6, 0, 99)
).foreach(pair => {
it(s"given ${pair._1} produces ${pair._2}") {
assert(new IntCode(pair._1).execute(Option.empty) == pair._2)
}
})
}
describe("Day 5 part 1") {
}
describe("Day 5 part 2") {
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment