Skip to content

Commit

Permalink
[2024/17] p1 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlitGhost committed Dec 17, 2024
1 parent 87d5888 commit 9252b53
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
Binary file added .aoc_tiles/tiles/2024/17.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions 2024/17/example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Register A: 729
Register B: 0
Register C: 0

Program: 0,1,5,4,3,0
5 changes: 5 additions & 0 deletions 2024/17/example_p2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Register A: 2024
Register B: 0
Register C: 0

Program: 0,3,5,4,3,0
82 changes: 82 additions & 0 deletions 2024/17/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from GhostyUtils import aoc
from dataclasses import dataclass
import operator
from typing import Callable


@dataclass
class Registers:
A: int
B: int
C: int


def combo(operand: int, regs: Registers) -> int:
match operand:
case 0: return 0
case 1: return 1
case 2: return 2
case 3: return 3
case 4: return regs.A
case 5: return regs.B
case 6: return regs.C
case 7: assert False


def process(program: list[int], regs: Registers, output: Callable):
i_ptr = 0

while True:
if i_ptr >= len(program):
break

instr = program[i_ptr]
operand = program[i_ptr+1]

match instr:
case 0: # adv (A division by combo operand, store in A)
regs.A = regs.A // (2 ** combo(operand, regs))
i_ptr += 2
case 1: # bxl (bitwise xor of B and literal operand, store in B)
regs.B = operator.xor(regs.B, operand)
i_ptr += 2
case 2: # bst (combo operand modulo 8, store in B)
regs.B = combo(operand, regs) % 8
i_ptr += 2
case 3: # jnz (jump to literal operand if A non-zero)
if regs.A == 0:
i_ptr += 2
else:
i_ptr = operand
case 4: # bxc (bitwise xor of B and C, store in B)
regs.B = operator.xor(regs.B, regs.C)
i_ptr += 2
case 5: # out (output combo operand modulo 8)
output(combo(operand, regs) % 8)
i_ptr += 2
case 6: # bdv (A division by combo operand, store in B)
regs.B = regs.A // (2 ** combo(operand, regs))
i_ptr += 2
case 7: # cdv (A division by combo operand, store in C)
regs.C = regs.A // (2 ** combo(operand, regs))
i_ptr += 2


def read_regs(registers: str) -> Registers:
a, b, c = (r.split(': ') for r in registers.splitlines())
regs = Registers(int(a[1]), int(b[1]), int(c[1]))
return regs


def main():
registers, program = (section.strip() for section in aoc.read_sections())
regs = read_regs(registers)
program = list(map(int, program.split(': ')[1].split(',')))

output = []
process(program, regs, output=output.append)
print(f"p1: {','.join(map(str, output))}")


if __name__ == "__main__":
main()
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code

<!-- AOC TILES BEGIN -->
<h1 align="center">
Advent of Code - 192/482
Advent of Code - 193/484
</h1>
<h1 align="center">
2024 - 31 ⭐ - Python
2024 - 32 ⭐ - Python
</h1>
<a href="2024/1/script.py">
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
Expand Down Expand Up @@ -56,6 +56,9 @@ My solutions to the yearly Advents of Code
<a href="2024/16/script.py">
<img src=".aoc_tiles/tiles/2024/16.png" width="161px">
</a>
<a href="2024/17/script.py">
<img src=".aoc_tiles/tiles/2024/17.png" width="161px">
</a>
<h1 align="center">
2023 - 47 ⭐ - Python
</h1>
Expand Down

0 comments on commit 9252b53

Please sign in to comment.