mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 12:50:32 +01:00
Implement 2024 day 17 part 1
This commit is contained in:
60
2024/src/aoc/days/day17.py
Normal file
60
2024/src/aoc/days/day17.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import re
|
||||
|
||||
from . import SeparateRunner
|
||||
|
||||
|
||||
class DayRunner(SeparateRunner):
|
||||
@classmethod
|
||||
def part1(cls, input: str) -> str:
|
||||
numbers = re.findall(r"\d+", input)
|
||||
|
||||
register_a, register_b, register_c = map(int, numbers[:3])
|
||||
program = list(map(int, numbers[3:]))
|
||||
|
||||
ip = 0
|
||||
out = []
|
||||
|
||||
def combo(index: int) -> int:
|
||||
match program[index]:
|
||||
case 0:
|
||||
return 0
|
||||
case 1:
|
||||
return 1
|
||||
case 2:
|
||||
return 2
|
||||
case 3:
|
||||
return 3
|
||||
case 4:
|
||||
return register_a
|
||||
case 5:
|
||||
return register_b
|
||||
case 6:
|
||||
return register_c
|
||||
|
||||
while ip < len(program):
|
||||
match program[ip]:
|
||||
case 0: # adv
|
||||
register_a = register_a // 2 ** combo(ip + 1)
|
||||
case 1: # bxl
|
||||
register_b ^= program[ip + 1]
|
||||
case 2: # bst
|
||||
register_b = combo(ip + 1) & 0x7
|
||||
case 3: # jnz
|
||||
if register_a != 0:
|
||||
ip = program[ip + 1]
|
||||
continue
|
||||
case 4: # bxc
|
||||
register_b ^= register_c
|
||||
case 5: # out
|
||||
out.append(combo(ip + 1) & 7)
|
||||
case 6: # bdv
|
||||
register_b = register_a // 2 ** combo(ip + 1)
|
||||
case 7: # cdv
|
||||
register_c = register_a // 2 ** combo(ip + 1)
|
||||
ip += 2
|
||||
|
||||
return ",".join(map(str, out))
|
||||
|
||||
@classmethod
|
||||
def part2(cls, input: str) -> str:
|
||||
pass
|
||||
5
2024/tests/samples/17.txt
Normal file
5
2024/tests/samples/17.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
Register A: 729
|
||||
Register B: 0
|
||||
Register C: 0
|
||||
|
||||
Program: 0,1,5,4,3,0
|
||||
@@ -14,3 +14,14 @@ from . import get_data
|
||||
)
|
||||
def test_sample_part1(data: str, result: int) -> None:
|
||||
assert DayRunner.part1(data) == result
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"data,result",
|
||||
[
|
||||
(get_data(16, 1), 45),
|
||||
(get_data(16, 2), 64),
|
||||
],
|
||||
)
|
||||
def test_sample_part2(data: str, result: int) -> None:
|
||||
assert DayRunner.part2(data) == result
|
||||
|
||||
7
2024/tests/test_day17.py
Normal file
7
2024/tests/test_day17.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from aoc.days.day17 import DayRunner
|
||||
|
||||
from . import get_data
|
||||
|
||||
|
||||
def test_sample_part1() -> None:
|
||||
assert DayRunner.part1(get_data(17)) == "4,6,3,5,6,3,5,2,1,0"
|
||||
Reference in New Issue
Block a user