Skip to content

Commit c0d7923

Browse files
committed
Year 2015 Day 23
1 parent cd6ff5d commit c0d7923

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,4 @@ The minimal SBT project provides:
232232
| 20 | [Infinite Elves and Infinite Houses](https://adventofcode.com/2015/day/20) | [Source](src/main/scala/AdventOfCode2015/Day20.scala) |
233233
| 21 | [RPG Simulator 20XX](https://adventofcode.com/2015/day/21) | [Source](src/main/scala/AdventOfCode2015/Day21.scala) |
234234
| 22 | [Wizard Simulator 20XX](https://adventofcode.com/2015/day/22) | [Source](src/main/scala/AdventOfCode2015/Day22.scala) |
235+
| 23 | [Opening the Turing Lock](https://adventofcode.com/2015/day/23) | [Source](src/main/scala/AdventOfCode2015/Day23.scala) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
jio a, +22
2+
inc a
3+
tpl a
4+
tpl a
5+
tpl a
6+
inc a
7+
tpl a
8+
inc a
9+
tpl a
10+
inc a
11+
inc a
12+
tpl a
13+
inc a
14+
inc a
15+
tpl a
16+
inc a
17+
inc a
18+
tpl a
19+
inc a
20+
inc a
21+
tpl a
22+
jmp +19
23+
tpl a
24+
tpl a
25+
tpl a
26+
tpl a
27+
inc a
28+
inc a
29+
tpl a
30+
inc a
31+
tpl a
32+
inc a
33+
inc a
34+
tpl a
35+
inc a
36+
inc a
37+
tpl a
38+
inc a
39+
tpl a
40+
tpl a
41+
jio a, +8
42+
inc b
43+
jie a, +4
44+
tpl a
45+
inc a
46+
jmp +2
47+
hlf a
48+
jmp -7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package AdventOfCode2015
2+
3+
object Day23:
4+
sealed trait State
5+
case object Running extends State
6+
case object Halted extends State
7+
8+
case class Cpu(instructions: Seq[String], registers: Map[String, Int] = Map(), ip: Int = 0, state: State = Running):
9+
def next: Cpu = copy(ip = ip + 1)
10+
def read(key: String): Int = key.toIntOption.getOrElse(registers.getOrElse(key, 0))
11+
def write(key: String, value: Int): Cpu = next.copy(registers = registers.updated(key, value))
12+
def step: Cpu =
13+
if !instructions.indices.contains(ip) then return copy(state = Halted)
14+
val (op, src, dest) = instructions(ip).split("[, ]+") match
15+
case Array(op, src, dest) => (op, src, dest)
16+
case Array(op, dest) => (op, "", dest)
17+
op match
18+
case "hlf" => write(dest, read(dest) / 2)
19+
case "tpl" => write(dest, read(dest) * 3)
20+
case "inc" => write(dest, read(dest) + 1)
21+
case "jmp" => copy(ip = ip + read(dest))
22+
case "jie" => if read(src) % 2 == 0 then copy(ip = ip + read(dest)) else next
23+
case "jio" => if read(src) == 1 then copy(ip = ip + read(dest)) else next
24+
end Cpu
25+
26+
// Reverse engineering the code shows that it calculates the length of the
27+
// 3n + 1 sequence (https://en.wikipedia.org/wiki/Collatz_conjecture)
28+
// for one of two different numbers chosen depending on whether a is 0 or 1.
29+
def run(input: Seq[String], a: Int): Int =
30+
Iterator.iterate(Cpu(input, Map("a" -> a)))(_.step).dropWhile(_.state == Running).next().registers("b")
31+
32+
def part1(input: Seq[String]): Int = run(input, 0)
33+
34+
def part2(input: Seq[String]): Int = run(input, 1)
35+
36+
def main(args: Array[String]): Unit =
37+
val data = io.Source.fromResource("AdventOfCode2015/Day23.txt").getLines().toSeq
38+
println(part1(data))
39+
println(part2(data))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package AdventOfCode2015
2+
3+
import org.scalatest.funsuite.AnyFunSuite
4+
5+
class Day23Suite extends AnyFunSuite
6+
// No unit tests possible

0 commit comments

Comments
 (0)