Commit d4cd3fb0 authored by David's avatar David

Day 2 part 2

parent 94d474eb
...@@ -12,15 +12,12 @@ class IntCode(ops: List[Int]) { ...@@ -12,15 +12,12 @@ class IntCode(ops: List[Int]) {
@tailrec def process(acc: List[Int], opCount: Int): List[Int] = { @tailrec def process(acc: List[Int], opCount: Int): List[Int] = {
acc.drop(opCount * 4) match { acc.drop(opCount * 4) match {
case ADD :: in1 :: in2 :: dest :: xs => 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) val result = acc(in1) + acc(in2)
process(acc.updated(dest, result), opCount + 1) process(acc.updated(dest, result), opCount + 1)
case MULT :: in1 :: in2 :: dest :: xs => 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) val result = acc(in1) * acc(in2)
process(acc.updated(dest, result), opCount + 1) process(acc.updated(dest, result), opCount + 1)
case END :: xs => case END :: xs =>
println("IT'S OVER")
acc acc
//return the new list? //return the new list?
case _ => throw new Exception("Unknown opcode") case _ => throw new Exception("Unknown opcode")
...@@ -30,5 +27,4 @@ class IntCode(ops: List[Int]) { ...@@ -30,5 +27,4 @@ class IntCode(ops: List[Int]) {
process(ops, 0) process(ops, 0)
} }
} }
...@@ -9,4 +9,19 @@ object Puzzle2 extends App { ...@@ -9,4 +9,19 @@ object Puzzle2 extends App {
println("FINAL ANSWER: " + result.head) println("FINAL ANSWER: " + result.head)
//part 2
println("PART TWO:")
(0 to 99).inclusive.foreach( noun => {
(0 to 99).inclusive.foreach(verb => {
val attempt = app.updated(1, noun).updated(2, verb)
val result = new IntCode(attempt).execute()
if(result.head == 19690720) {
println(s"Noun = $noun verb = $verb")
println(s"ANSWER: ${100 * noun + verb}")
}
})
})
println("ALL DONE")
} }
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