Commit 94d474eb authored by David's avatar David

Day 2 part 1

parent 4d1e2967
package is.kow.adventofcode._2019
import scala.annotation.tailrec
class IntCode(ops: List[Int]) {
private final val ADD = 1
private final val MULT = 2
private final val END = 99
def execute(): List[Int] = {
@tailrec def process(acc: List[Int], opCount: Int): List[Int] = {
acc.drop(opCount * 4) match {
case ADD :: in1 :: in2 :: dest :: xs =>
println(s"add ${acc(in1)} to ${acc(in2)} and put at $dest -- $xs")
val result = acc(in1) + acc(in2)
process(acc.updated(dest, result), opCount + 1)
case MULT :: in1 :: in2 :: dest :: xs =>
println(s"mult ${acc(in1)} and ${acc(in2)} and put at $dest -- $xs")
val result = acc(in1) * acc(in2)
process(acc.updated(dest, result), opCount + 1)
case END :: xs =>
println("IT'S OVER")
acc
//return the new list?
case _ => throw new Exception("Unknown opcode")
}
}
process(ops, 0)
}
}
package is.kow.adventofcode._2019
object Puzzle2 extends App {
val app = List(1, 0, 0, 3, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 10, 1, 19, 1, 19, 9, 23, 1, 23, 6, 27, 1, 9, 27, 31, 1, 31, 10, 35, 2, 13, 35, 39, 1, 39, 10, 43, 1, 43, 9, 47, 1, 47, 13, 51, 1, 51, 13, 55, 2, 55, 6, 59, 1, 59, 5, 63, 2, 10, 63, 67, 1, 67, 9, 71, 1, 71, 13, 75, 1, 6, 75, 79, 1, 10, 79, 83, 2, 9, 83, 87, 1, 87, 5, 91, 2, 91, 9, 95, 1, 6, 95, 99, 1, 99, 5, 103, 2, 103, 10, 107, 1, 107, 6, 111, 2, 9, 111, 115, 2, 9, 115, 119, 2, 13, 119, 123, 1, 123, 9, 127, 1, 5, 127, 131, 1, 131, 2, 135, 1, 135, 6, 0, 99, 2, 0, 14, 0)
val fixed = app.updated(1, 12).updated(2, 2)
val result = new IntCode(fixed).execute()
println("FINAL ANSWER: " + result.head)
}
package _2019
import is.kow.adventofcode._2019.IntCode
import org.junit.runner.RunWith
import org.scalatest.funspec.AnyFunSpec
import org.scalatestplus.junit.JUnitRunner
@RunWith(classOf[JUnitRunner])
class Puzzle2Spec extends AnyFunSpec {
describe("Day 2: part 1") {
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() == pair._2)
}
})
}
describe("Puzzle 4: part 2") {
}
}
include("1")
include("4")
include("1", "2", "4")
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