Skip to content

Commit

Permalink
feat: add 2023 day 07 part 01
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelblijleven committed Dec 8, 2023
1 parent 4f08dd9 commit 0b742e0
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/adventofcode/year_2023/day_08_2023.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import re
from collections import deque

from adventofcode.util.exceptions import SolutionNotFoundError
from adventofcode.registry.decorators import register_solution
from adventofcode.util.input_helpers import get_input_for_day


def parse_input(data: list[str]) -> tuple[deque, dict[str, tuple[str, str]]]:
instructions = deque(data[0])
paths: dict[str, tuple[str, str]] = {}
pattern = re.compile("[A-Z]{3}")

for line in data[2:]:
key, left, right = pattern.findall(line)
paths[key] = (left, right)

return instructions, paths


def follow_instructions(data: list[str]) -> int:
instructions, paths = parse_input(data)
current_position = "AAA"
steps = 0

while True:
direction = 0 if instructions[0] == "L" else 1
current_position = paths[current_position][direction]
steps += 1

if current_position == "ZZZ":
break

instructions.rotate(-1)

return steps


@register_solution(2023, 8, 1)
def part_one(input_data: list[str]):
answer = follow_instructions(input_data)

if not answer:
raise SolutionNotFoundError(2023, 8, 1)

return answer


@register_solution(2023, 8, 2)
def part_two(input_data: list[str]):
answer = ...

if not answer:
raise SolutionNotFoundError(2023, 8, 2)

return answer


if __name__ == '__main__':
data = get_input_for_day(2023, 8)
part_one(data)
part_two(data)
46 changes: 46 additions & 0 deletions tests/adventofcode/year_2023/test_day_08_2023.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from collections import deque

from adventofcode.year_2023.day_08_2023 import part_two, part_one, parse_input, follow_instructions



test_input = [
"RL",
"",
"AAA = (BBB, CCC)",
"BBB = (DDD, EEE)",
"CCC = (ZZZ, GGG)",
"DDD = (DDD, DDD)",
"EEE = (EEE, EEE)",
"GGG = (GGG, GGG)",
"ZZZ = (ZZZ, ZZZ)",
]

test_input_2 = [
"LLR",
"",
"AAA = (BBB, BBB)",
"BBB = (AAA, ZZZ)",
"ZZZ = (ZZZ, ZZZ)",
]


def test_parse_input():
assert parse_input(test_input_2) == (deque("LLR"), {
"AAA": ("BBB", "BBB"),
"BBB": ("AAA", "ZZZ"),
"ZZZ": ("ZZZ", "ZZZ"),
})


def test_follow_instructions():
assert follow_instructions(test_input) == 2
assert follow_instructions(test_input_2) == 6


def test_part_one():
assert part_one(test_input_2) == 6


def test_part_two():
assert part_two(test_input) == 'x'

0 comments on commit 0b742e0

Please sign in to comment.