Skip to content

Commit b5a3fd2

Browse files
committed
Year 2019 Day 9
1 parent 7abf595 commit b5a3fd2

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,4 @@ Entries for the annual [Advent of Code](https://adventofcode.com/) challenge, wr
7676
| 6 | [Universal Orbit Map](https://adventofcode.com/2019/day/6) | [Source](src/main/scala/AdventOfCode2019/Day06.scala) |
7777
| 7 | [Amplification Circuit](https://adventofcode.com/2019/day/7) | [Source](src/main/scala/AdventOfCode2019/Day07.scala) |
7878
| 8 | [Space Image Format](https://adventofcode.com/2019/day/8) | [Source](src/main/scala/AdventOfCode2019/Day08.scala) |
79+
| 9 | [Sensor Boost](https://adventofcode.com/2019/day/9) | [Source](src/main/scala/AdventOfCode2019/Day09.scala) |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1102,34463338,34463338,63,1007,63,34463338,63,1005,63,53,1101,3,0,1000,109,988,209,12,9,1000,209,6,209,3,203,0,1008,1000,1,63,1005,63,65,1008,1000,2,63,1005,63,904,1008,1000,0,63,1005,63,58,4,25,104,0,99,4,0,104,0,99,4,17,104,0,99,0,0,1101,27,0,1014,1101,286,0,1023,1102,1,35,1018,1102,20,1,1000,1101,26,0,1010,1101,0,289,1022,1102,1,30,1019,1102,734,1,1025,1102,1,31,1012,1101,25,0,1001,1102,1,1,1021,1101,0,36,1002,1101,0,527,1028,1101,895,0,1026,1102,1,23,1016,1101,21,0,1003,1102,22,1,1011,1102,1,522,1029,1102,1,892,1027,1102,1,0,1020,1102,1,28,1015,1102,38,1,1006,1101,0,32,1008,1101,743,0,1024,1101,0,37,1007,1102,1,24,1013,1102,1,33,1009,1102,39,1,1004,1102,1,34,1005,1102,1,29,1017,109,19,21102,40,1,-3,1008,1016,40,63,1005,63,203,4,187,1106,0,207,1001,64,1,64,1002,64,2,64,109,-7,2101,0,-7,63,1008,63,32,63,1005,63,227,1106,0,233,4,213,1001,64,1,64,1002,64,2,64,109,-3,2108,37,-2,63,1005,63,255,4,239,1001,64,1,64,1105,1,255,1002,64,2,64,109,11,21108,41,40,-6,1005,1014,275,1001,64,1,64,1106,0,277,4,261,1002,64,2,64,109,10,2105,1,-7,1105,1,295,4,283,1001,64,1,64,1002,64,2,64,109,-27,1201,-2,0,63,1008,63,25,63,1005,63,321,4,301,1001,64,1,64,1105,1,321,1002,64,2,64,109,15,21107,42,41,0,1005,1018,341,1001,64,1,64,1106,0,343,4,327,1002,64,2,64,109,-25,2108,20,10,63,1005,63,359,1105,1,365,4,349,1001,64,1,64,1002,64,2,64,109,12,2107,35,0,63,1005,63,385,1001,64,1,64,1106,0,387,4,371,1002,64,2,64,109,4,21101,43,0,6,1008,1015,43,63,1005,63,409,4,393,1106,0,413,1001,64,1,64,1002,64,2,64,109,9,21101,44,0,-8,1008,1010,46,63,1005,63,437,1001,64,1,64,1106,0,439,4,419,1002,64,2,64,109,5,21108,45,45,-4,1005,1019,457,4,445,1106,0,461,1001,64,1,64,1002,64,2,64,109,-22,2102,1,7,63,1008,63,33,63,1005,63,481,1106,0,487,4,467,1001,64,1,64,1002,64,2,64,109,14,21102,46,1,-1,1008,1014,43,63,1005,63,507,1106,0,513,4,493,1001,64,1,64,1002,64,2,64,109,12,2106,0,1,4,519,1106,0,531,1001,64,1,64,1002,64,2,64,109,-17,1205,10,547,1001,64,1,64,1106,0,549,4,537,1002,64,2,64,109,-8,1202,-2,1,63,1008,63,17,63,1005,63,569,1105,1,575,4,555,1001,64,1,64,1002,64,2,64,109,23,1206,-5,593,4,581,1001,64,1,64,1105,1,593,1002,64,2,64,109,-14,1208,-8,24,63,1005,63,613,1001,64,1,64,1105,1,615,4,599,1002,64,2,64,109,-2,1207,-1,33,63,1005,63,633,4,621,1105,1,637,1001,64,1,64,1002,64,2,64,109,2,21107,47,48,5,1005,1016,659,4,643,1001,64,1,64,1105,1,659,1002,64,2,64,109,-11,1208,8,32,63,1005,63,681,4,665,1001,64,1,64,1106,0,681,1002,64,2,64,109,2,2101,0,0,63,1008,63,36,63,1005,63,703,4,687,1106,0,707,1001,64,1,64,1002,64,2,64,109,12,1206,7,719,1106,0,725,4,713,1001,64,1,64,1002,64,2,64,109,2,2105,1,8,4,731,1001,64,1,64,1106,0,743,1002,64,2,64,109,-21,2102,1,9,63,1008,63,39,63,1005,63,769,4,749,1001,64,1,64,1105,1,769,1002,64,2,64,109,11,1201,-3,0,63,1008,63,24,63,1005,63,793,1001,64,1,64,1105,1,795,4,775,1002,64,2,64,109,20,1205,-5,809,4,801,1105,1,813,1001,64,1,64,1002,64,2,64,109,-23,1207,4,36,63,1005,63,833,1001,64,1,64,1105,1,835,4,819,1002,64,2,64,109,-3,2107,33,5,63,1005,63,853,4,841,1106,0,857,1001,64,1,64,1002,64,2,64,109,16,1202,-9,1,63,1008,63,37,63,1005,63,879,4,863,1105,1,883,1001,64,1,64,1002,64,2,64,109,12,2106,0,-1,1105,1,901,4,889,1001,64,1,64,4,64,99,21101,0,27,1,21101,0,915,0,1106,0,922,21201,1,48476,1,204,1,99,109,3,1207,-2,3,63,1005,63,964,21201,-2,-1,1,21101,0,942,0,1105,1,922,21202,1,1,-1,21201,-2,-3,1,21101,0,957,0,1105,1,922,22201,1,-1,-2,1106,0,968,21202,-2,1,-2,109,-3,2106,0,0
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package AdventOfCode2019
2+
3+
object Day09:
4+
object IntCode:
5+
val powers = Map(1 -> 100, 2 -> 1000, 3 -> 10000)
6+
7+
sealed trait State
8+
case object Initial extends State
9+
case object Running extends State
10+
case object Halted extends State
11+
case class Output(value: Long) extends State
12+
13+
def apply(input: Seq[Long]): IntCode =
14+
val memory = input.zipWithIndex.map((value, index) => index.toLong -> value)
15+
IntCode(0, 0, memory.toMap.withDefaultValue(0), Seq(), Initial)
16+
end IntCode
17+
18+
case class IntCode(ip: Long, relativeBase: Long, memory: Map[Long, Long], input: Seq[Long], result: IntCode.State):
19+
import IntCode._
20+
21+
private def next: IntCode = memory(ip) % 100 match
22+
case 1 => copy(ip = ip + 4, memory = write(3, read(1) + read(2)), result = Running) // Add
23+
case 2 => copy(ip = ip + 4, memory = write(3, read(1) * read(2)), result = Running) // Multiply
24+
case 3 => copy(ip = ip + 2, memory = write(1, input.head), input = input.tail, result = Running) // Read
25+
case 4 => copy(ip = ip + 2, result = Output(read(1))) // Write
26+
case 5 => copy(ip = if read(1) != 0 then read(2) else ip + 3, result = Running) // Jump if true
27+
case 6 => copy(ip = if read(1) == 0 then read(2) else ip + 3, result = Running) // Jump if false
28+
case 7 => copy(ip = ip + 4, memory = write(3, if read(1) < read(2) then 1 else 0), result = Running) // Less than
29+
case 8 => copy(ip = ip + 4, memory = write(3, if read(1) == read(2) then 1 else 0), result = Running) // Equals
30+
case 9 => copy(ip = ip + 2, relativeBase = relativeBase + read(1), result = Running) // Relative base
31+
case 99 => copy(result = Halted) // Halt
32+
33+
private def read(offset: Int): Long = (memory(ip) / powers(offset)) % 10 match
34+
case 0 => memory(memory(ip + offset))
35+
case 1 => memory(ip + offset)
36+
case 2 => memory(relativeBase + memory(ip + offset))
37+
38+
private def write(offset: Int, value: Long): Map[Long, Long] = (memory(ip) / powers(offset)) % 10 match
39+
case 0 => memory.updated(memory(ip + offset), value)
40+
case 2 => memory.updated(relativeBase + memory(ip + offset), value)
41+
42+
def withInput(next: Long*): IntCode = copy(input = input.appendedAll(next))
43+
44+
def nextOutput: IntCode = Iterator.iterate(next)(_.next).dropWhile(_.result == Running).next()
45+
46+
def lastOutput: Long =
47+
val output = Iterator.iterate(this)(_.nextOutput).takeWhile(_.result != Halted)
48+
output.toSeq.map(_.result).collect { case Output(value) => value }.last
49+
end IntCode
50+
51+
def part1(memory: Seq[Long]): Long = IntCode(memory).withInput(1).lastOutput
52+
53+
def part2(memory: Seq[Long]): Long = IntCode(memory).withInput(2).lastOutput
54+
55+
def main(args: Array[String]): Unit =
56+
val data = io.Source.fromResource("AdventOfCode2019/Day09.txt").mkString.trim.split(",").map(_.toLong).toSeq
57+
println(part1(data))
58+
println(part2(data))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package AdventOfCode2019
2+
3+
import org.scalatest.funsuite.AnyFunSuite
4+
5+
class Day09Suite extends AnyFunSuite:
6+
val sample = Seq(1102,34915192,34915192,7,4,7,99,0).map(_.toLong)
7+
8+
test("Part 1 should handle sample input correctly") {
9+
assert(Day09.part1(sample) == 1219070632396864L)
10+
}

0 commit comments

Comments
 (0)