Skip to content

Commit 9252b53

Browse files
committed
[2024/17] p1 solved
1 parent 87d5888 commit 9252b53

File tree

5 files changed

+97
-2
lines changed

5 files changed

+97
-2
lines changed

.aoc_tiles/tiles/2024/17.png

4.39 KB
Loading

2024/17/example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Register A: 729
2+
Register B: 0
3+
Register C: 0
4+
5+
Program: 0,1,5,4,3,0

2024/17/example_p2

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Register A: 2024
2+
Register B: 0
3+
Register C: 0
4+
5+
Program: 0,3,5,4,3,0

2024/17/script.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from GhostyUtils import aoc
2+
from dataclasses import dataclass
3+
import operator
4+
from typing import Callable
5+
6+
7+
@dataclass
8+
class Registers:
9+
A: int
10+
B: int
11+
C: int
12+
13+
14+
def combo(operand: int, regs: Registers) -> int:
15+
match operand:
16+
case 0: return 0
17+
case 1: return 1
18+
case 2: return 2
19+
case 3: return 3
20+
case 4: return regs.A
21+
case 5: return regs.B
22+
case 6: return regs.C
23+
case 7: assert False
24+
25+
26+
def process(program: list[int], regs: Registers, output: Callable):
27+
i_ptr = 0
28+
29+
while True:
30+
if i_ptr >= len(program):
31+
break
32+
33+
instr = program[i_ptr]
34+
operand = program[i_ptr+1]
35+
36+
match instr:
37+
case 0: # adv (A division by combo operand, store in A)
38+
regs.A = regs.A // (2 ** combo(operand, regs))
39+
i_ptr += 2
40+
case 1: # bxl (bitwise xor of B and literal operand, store in B)
41+
regs.B = operator.xor(regs.B, operand)
42+
i_ptr += 2
43+
case 2: # bst (combo operand modulo 8, store in B)
44+
regs.B = combo(operand, regs) % 8
45+
i_ptr += 2
46+
case 3: # jnz (jump to literal operand if A non-zero)
47+
if regs.A == 0:
48+
i_ptr += 2
49+
else:
50+
i_ptr = operand
51+
case 4: # bxc (bitwise xor of B and C, store in B)
52+
regs.B = operator.xor(regs.B, regs.C)
53+
i_ptr += 2
54+
case 5: # out (output combo operand modulo 8)
55+
output(combo(operand, regs) % 8)
56+
i_ptr += 2
57+
case 6: # bdv (A division by combo operand, store in B)
58+
regs.B = regs.A // (2 ** combo(operand, regs))
59+
i_ptr += 2
60+
case 7: # cdv (A division by combo operand, store in C)
61+
regs.C = regs.A // (2 ** combo(operand, regs))
62+
i_ptr += 2
63+
64+
65+
def read_regs(registers: str) -> Registers:
66+
a, b, c = (r.split(': ') for r in registers.splitlines())
67+
regs = Registers(int(a[1]), int(b[1]), int(c[1]))
68+
return regs
69+
70+
71+
def main():
72+
registers, program = (section.strip() for section in aoc.read_sections())
73+
regs = read_regs(registers)
74+
program = list(map(int, program.split(': ')[1].split(',')))
75+
76+
output = []
77+
process(program, regs, output=output.append)
78+
print(f"p1: {','.join(map(str, output))}")
79+
80+
81+
if __name__ == "__main__":
82+
main()

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code
33

44
<!-- AOC TILES BEGIN -->
55
<h1 align="center">
6-
Advent of Code - 192/482
6+
Advent of Code - 193/484
77
</h1>
88
<h1 align="center">
9-
2024 - 31 ⭐ - Python
9+
2024 - 32 ⭐ - Python
1010
</h1>
1111
<a href="2024/1/script.py">
1212
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
@@ -56,6 +56,9 @@ My solutions to the yearly Advents of Code
5656
<a href="2024/16/script.py">
5757
<img src=".aoc_tiles/tiles/2024/16.png" width="161px">
5858
</a>
59+
<a href="2024/17/script.py">
60+
<img src=".aoc_tiles/tiles/2024/17.png" width="161px">
61+
</a>
5962
<h1 align="center">
6063
2023 - 47 ⭐ - Python
6164
</h1>

0 commit comments

Comments
 (0)