-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f08dd9
commit 0b742e0
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |