Skip to content

Commit 9f43a79

Browse files
committed
Year 2019 Day 7
1 parent 36526d3 commit 9f43a79

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,4 @@ Entries for the annual [Advent of Code](https://adventofcode.com/) challenge, wr
7474
| 4 | [Secure Container](https://adventofcode.com/2019/day/4) | [Source](src/main/scala/AdventOfCode2019/Day04.scala) |
7575
| 5 | [Sunny with a Chance of Asteroids](https://adventofcode.com/2019/day/5) | [Source](src/main/scala/AdventOfCode2019/Day05.scala) |
7676
| 6 | [Universal Orbit Map](https://adventofcode.com/2019/day/6) | [Source](src/main/scala/AdventOfCode2019/Day06.scala) |
77+
| 7 | [Amplification Circuit](https://adventofcode.com/2019/day/7) | [Source](src/main/scala/AdventOfCode2019/Day07.scala) |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3,8,1001,8,10,8,105,1,0,0,21,46,59,84,93,102,183,264,345,426,99999,3,9,1002,9,4,9,1001,9,3,9,102,2,9,9,1001,9,5,9,102,3,9,9,4,9,99,3,9,1002,9,3,9,101,4,9,9,4,9,99,3,9,1002,9,4,9,1001,9,4,9,102,2,9,9,1001,9,2,9,1002,9,3,9,4,9,99,3,9,1001,9,5,9,4,9,99,3,9,1002,9,4,9,4,9,99,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,99
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package AdventOfCode2019
2+
3+
import scala.annotation.tailrec
4+
5+
object Day07:
6+
object IntCode:
7+
val powers = Map(1 -> 100, 2 -> 1000)
8+
9+
sealed trait State
10+
case object Initial extends State
11+
case object Running extends State
12+
case object Halted extends State
13+
case class Output(value: Int) extends State
14+
15+
def apply(code: Seq[Int]): IntCode = IntCode(0, code, Seq(), Initial)
16+
end IntCode
17+
18+
case class IntCode(ip: Int, code: Seq[Int], input: Seq[Int], result: IntCode.State):
19+
import IntCode._
20+
21+
private def next: IntCode = code(ip) % 100 match
22+
case 1 => IntCode(ip + 4, write(3, read(1) + read(2)), input, Running) // Add
23+
case 2 => IntCode(ip + 4, write(3, read(1) * read(2)), input, Running) // Multiply
24+
case 3 => IntCode(ip + 2, write(1, input.head), input.tail, Running) // Read
25+
case 4 => IntCode(ip + 2, code, input, Output(read(1))) // Write
26+
case 5 => IntCode(if read(1) != 0 then read(2) else ip + 3, code, input, Running) // Jump if true
27+
case 6 => IntCode(if read(1) == 0 then read(2) else ip + 3, code, input, Running) // Jump if false
28+
case 7 => IntCode(ip + 4, write(3, if read(1) < read(2) then 1 else 0), input, Running) // Less than
29+
case 8 => IntCode(ip + 4, write(3, if read(1) == read(2) then 1 else 0), input, Running) // Equals
30+
case 99 => IntCode(ip, code, input, Halted) // Halt
31+
32+
private def read(offset: Int): Int = (code(ip) / powers(offset)) % 10 match
33+
case 0 => code(code(ip + offset))
34+
case 1 => code(ip + offset)
35+
36+
private def write(offset: Int, value: Int): Seq[Int] = code.updated(code(ip + offset), value)
37+
38+
def withInput(next: Int*): IntCode = copy(input = input.appendedAll(next))
39+
40+
def nextOutput: IntCode = Iterator.iterate(next)(_.next).dropWhile(_.result == Running).next()
41+
42+
def allOutput: Seq[Int] =
43+
val output = Iterator.iterate(this)(_.nextOutput).takeWhile(_.result != Halted)
44+
output.toSeq.map(_.result).collect { case Output(value) => value }
45+
end IntCode
46+
47+
def part1(code: Seq[Int]): Int = (0 to 4).permutations.map(_.foldLeft(0)((total, next) => IntCode(code).withInput(next, total).allOutput.last)).max
48+
49+
def part2(code: Seq[Int]): Int =
50+
@tailrec
51+
def helper(previousOutput: Int)(amps: Seq[IntCode]): Int =
52+
val nextAmp = amps.head.withInput(previousOutput).nextOutput
53+
nextAmp.result match
54+
case IntCode.Output(nextOutput) => helper(nextOutput)(amps.tail.appended(nextAmp))
55+
case _ => previousOutput
56+
57+
(5 to 9).map(phase => IntCode(code).withInput(phase)).permutations.map(helper(0)).max
58+
end part2
59+
60+
def main(args: Array[String]): Unit =
61+
val data = io.Source.fromResource("AdventOfCode2019/Day07.txt").mkString.trim.split(",").map(_.toInt).toSeq
62+
println(part1(data))
63+
println(part2(data))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package AdventOfCode2019
2+
3+
import org.scalatest.funsuite.AnyFunSuite
4+
5+
class Day07Suite extends AnyFunSuite:
6+
val sample1 = Seq(3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0)
7+
8+
test("Part 1 should handle sample input correctly") {
9+
assert(Day07.part1(sample1) == 65210)
10+
}
11+
12+
val sample2 = Seq(3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5)
13+
14+
test("Part 2 should handle sample input correctly") {
15+
assert(Day07.part2(sample2) == 139629729)
16+
}

0 commit comments

Comments
 (0)