Skip to content

Commit 65df475

Browse files
committed
2024 day 17
1 parent 49ef462 commit 65df475

5 files changed

Lines changed: 92 additions & 3 deletions

File tree

2024/python/data/day17/example.txt

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/python/data/day17/input.txt

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

2024/python/src/aoc2024/day17.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from pathlib import Path
2+
3+
4+
def part_1(input: Path) -> str:
5+
lines = input.read_text().splitlines()
6+
a = int(lines[0].split(":")[1].strip())
7+
b = int(lines[1].split(":")[1].strip())
8+
c = int(lines[2].split(":")[1].strip())
9+
program = list(map(int, lines[4].split(":")[1].strip().split(",")))
10+
return run_program(program, a, b, c)
11+
12+
13+
def run_program(program: list[int], a: int, b: int, c: int) -> str:
14+
def literal(operand: int) -> int:
15+
return operand
16+
17+
def combo(operand: int) -> int:
18+
match operand:
19+
case 0 | 1 | 2 | 3:
20+
return operand
21+
case 4:
22+
return a
23+
case 5:
24+
return b
25+
case 6:
26+
return c
27+
case _:
28+
raise ValueError(f"Invalid combo operand: {operand}")
29+
30+
ip = 0
31+
output: list[int] = []
32+
while ip < len(program):
33+
opcode = program[ip]
34+
operand = program[ip + 1]
35+
match opcode:
36+
case 0:
37+
numerator = a
38+
denominator = 2 ** combo(operand)
39+
a = numerator // denominator
40+
ip += 2
41+
case 1:
42+
b = b ^ literal(operand)
43+
ip += 2
44+
case 2:
45+
b = combo(operand) % 8
46+
ip += 2
47+
case 3:
48+
if a == 0:
49+
ip += 2
50+
else:
51+
ip = literal(operand)
52+
case 4:
53+
b = b ^ c
54+
# operand unused.
55+
ip += 2
56+
case 5:
57+
output.append(combo(operand) % 8)
58+
ip += 2
59+
case 6:
60+
numerator = a
61+
denominator = 2 ** combo(operand)
62+
b = numerator // denominator
63+
ip += 2
64+
case 7:
65+
numerator = a
66+
denominator = 2 ** combo(operand)
67+
c = numerator // denominator
68+
ip += 2
69+
return ",".join(map(str, output))

2024/python/tests/test_day17.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from pathlib import Path
2+
3+
from aoc2024 import day17
4+
5+
DATA_DIR = Path(__file__).parent.parent / "data/day17"
6+
7+
8+
def test_part_1():
9+
assert day17.part_1(DATA_DIR / "example.txt") == "4,6,3,5,6,3,5,2,1,0"
10+
assert day17.part_1(DATA_DIR / "input.txt") == "7,0,7,3,4,1,3,0,1"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ https://adventofcode.com
55
## Progress
66

77
<!-- PROGRESS_START -->
8-
Updated 2026-01-05.
8+
Updated 2026-01-06.
99

1010
| **Year** | 🌟 | **1** | **2** | **3** | **4** | **5** | **6** | **7** | **8** | **9** | **10** | **11** | **12** | **13** | **14** | **15** | **16** | **17** | **18** | **19** | **20** | **21** | **22** | **23** | **24** | **25** |
1111
|------------|------------------|---------|---------|---------|---------|---------|---------|---------|---------|---------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|
@@ -18,8 +18,8 @@ Updated 2026-01-05.
1818
| **2021** | **50/50**&nbsp;🌟 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 |
1919
| **2022** | **50/50**&nbsp;🌟 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 |
2020
| **2023** | 42/50 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 || 🟩 ||||
21-
| **2024** | 32/50 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | |||||||||
21+
| **2024** | 33/50 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟨 |||||||||
2222
| **2025** | **24/24**&nbsp;🌟 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 | 🟩 ||||||||||||||
2323

24-
Total stars: 346/524
24+
Total stars: 347/524
2525
<!-- PROGRESS_END -->

0 commit comments

Comments
 (0)