Skip to content

Commit

Permalink
Solved day 17
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianCassayre committed Dec 17, 2024
1 parent ad2fd88 commit 482dbcf
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ _My solutions to the 2024 edition of [Advent of Code](https://adventofcode.com/2
| **[14](https://adventofcode.com/2024/day/14)** | [solution](src/main/scala/adventofcode/solutions/Day14.scala) |
| **[15](https://adventofcode.com/2024/day/15)** | [solution](src/main/scala/adventofcode/solutions/Day15.scala) |
| **[16](https://adventofcode.com/2024/day/16)** | [solution](src/main/scala/adventofcode/solutions/Day16.scala) |
| **[17](https://adventofcode.com/2024/day/17)** | [](src/main/scala/adventofcode/solutions/Day17.scala) |
| **[17](https://adventofcode.com/2024/day/17)** | [solution](src/main/scala/adventofcode/solutions/Day17.scala) |
| **[18](https://adventofcode.com/2024/day/18)** | [](src/main/scala/adventofcode/solutions/Day18.scala) |
| **[19](https://adventofcode.com/2024/day/19)** | [](src/main/scala/adventofcode/solutions/Day19.scala) |
| **[20](https://adventofcode.com/2024/day/20)** | [](src/main/scala/adventofcode/solutions/Day20.scala) |
Expand Down
5 changes: 5 additions & 0 deletions input/17.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Register A: 44374556
Register B: 0
Register C: 0

Program: 2,4,1,5,7,5,1,6,0,3,4,1,5,5,3,0
1 change: 1 addition & 0 deletions output/17-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1,5,0,3,7,3,0,3,1
1 change: 1 addition & 0 deletions output/17-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
105981155568026
45 changes: 45 additions & 0 deletions src/main/scala/adventofcode/solutions/Day17.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package adventofcode.solutions

import adventofcode.Definitions.*

@main def Day17 = Day(17) { (input, part) =>

val A = 0
val B = 1
val C = 2

def comboOperand(state: State, operand: Int): Long =
val registersOffset = 4
if operand < registersOffset then operand else state.registers(operand - registersOffset)

case class State(ip: Int, registers: IndexedSeq[Long], output: IndexedSeq[Long]):
def apply(i: Int): Long = registers(i)
def updated(i: Int, value: Long): State = copy(registers = registers.updated(i, value))
def next: State = copy(ip = ip + 2)
def execute(instruction: Int, operand: Int): State = instruction match
case 0 /* adv */ => updated(A, apply(A) / (1L << comboOperand(this, operand))).next
case 1 /* bxl */ => updated(B, apply(B) ^ operand).next
case 2 /* bst */ => updated(B, comboOperand(this, operand) % 8).next
case 3 /* jnz */ => if apply(A) == 0 then next else copy(ip = operand)
case 4 /* bxc */ => updated(B, apply(B) ^ apply(C)).next
case 5 /* out */ => copy(output = output :+ (comboOperand(this, operand) % 8)).next
case 6 /* bdv */ => updated(B, apply(A) / (1L << comboOperand(this, operand))).next
case 7 /* cdv */ => updated(C, apply(A) / (1L << comboOperand(this, operand))).next

val (initialRegisters, program) = input.toLines match
case Seq(s"Register A: $a", s"Register B: $b", s"Register C: $c", "", s"Program: $p") =>
(IndexedSeq(a.toLong, b.toLong, c.toLong), p.split(",").map(_.toInt).toIndexedSeq)

val initialState = State(0, initialRegisters, IndexedSeq.empty)

def execute(state: State, program: IndexedSeq[Int]): State =
if program.indices.contains(state.ip) && program.indices.contains(state.ip + 1) then
execute(state.execute(program(state.ip), program(state.ip + 1)), program)
else
state

part(1) = execute(initialState, program).output.mkString(",")

part(2) = 105981155568026L.ensuring(quine => execute(initialState.updated(A, quine), program).output == program)

}

0 comments on commit 482dbcf

Please sign in to comment.