You've already forked advent_of_code
feat: Redo of the AoC repo starting over with 2015 and keeping up with
2025 (completed day 2 so far)
This commit is contained in:
BIN
15/3/__pycache__/main.cpython-313.pyc
Normal file
BIN
15/3/__pycache__/main.cpython-313.pyc
Normal file
Binary file not shown.
BIN
15/3/__pycache__/test_day3.cpython-313-pytest-9.0.1.pyc
Normal file
BIN
15/3/__pycache__/test_day3.cpython-313-pytest-9.0.1.pyc
Normal file
Binary file not shown.
66
15/3/main.py
Normal file
66
15/3/main.py
Normal file
@@ -0,0 +1,66 @@
|
||||
'''AoC day 2'''
|
||||
|
||||
def get_new_pos(curr_pos, instruction):
|
||||
if instruction == '<':
|
||||
new_pos = (curr_pos[0] - 1, curr_pos[1])
|
||||
elif instruction == '>':
|
||||
new_pos = (curr_pos[0] + 1, curr_pos[1])
|
||||
elif instruction == '^':
|
||||
new_pos = (curr_pos[0], curr_pos[1] + 1)
|
||||
elif instruction == 'v':
|
||||
new_pos = (curr_pos[0], curr_pos[1] - 1)
|
||||
else:
|
||||
new_pos = (0, 0)
|
||||
|
||||
return new_pos
|
||||
|
||||
|
||||
def process_instructions(instruction_set: str) -> int:
|
||||
'''Run through a set of instructions to return the number of unique houses visited
|
||||
|
||||
Keyword args:
|
||||
instruction_set (str) - Directional arrow instruction string giving moves to new locations
|
||||
|
||||
Returns:
|
||||
int - Number of unique houses visited
|
||||
'''
|
||||
visited_house_coordinates = {(0,0)}
|
||||
curr_pos = (0, 0)
|
||||
|
||||
for instruction in instruction_set:
|
||||
curr_pos = get_new_pos(curr_pos, instruction)
|
||||
|
||||
visited_house_coordinates.add(curr_pos)
|
||||
|
||||
return len(visited_house_coordinates)
|
||||
|
||||
def process_robo_instructions(instruction_set: str) -> int:
|
||||
visited_house_coordinates = {(0,0)}
|
||||
santa_pos = (0,0)
|
||||
robot_pos = (0,0)
|
||||
|
||||
for index, instruction in enumerate(instruction_set):
|
||||
if index % 2 == 0:
|
||||
santa_pos = get_new_pos(santa_pos, instruction)
|
||||
visited_house_coordinates.add(santa_pos)
|
||||
else:
|
||||
robot_pos = get_new_pos(robot_pos, instruction)
|
||||
visited_house_coordinates.add(robot_pos)
|
||||
|
||||
return len(visited_house_coordinates)
|
||||
|
||||
def main():
|
||||
'''Entrypoint'''
|
||||
with open('input.txt', encoding='utf-8') as fh:
|
||||
instruction_set = fh.readline()
|
||||
unique_houses = process_instructions(instruction_set)
|
||||
|
||||
print(f'Part1: {unique_houses}')
|
||||
|
||||
robo_houses = process_robo_instructions(instruction_set)
|
||||
|
||||
print(f'Part2: {robo_houses}')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
20
15/3/test_day3.py
Normal file
20
15/3/test_day3.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import pytest
|
||||
|
||||
from main import process_instructions, process_robo_instructions
|
||||
|
||||
@pytest.mark.parametrize('given, expected',
|
||||
[
|
||||
('>', 2),
|
||||
('^>v<', 4),
|
||||
('^v^v^v^v^v', 2)
|
||||
])
|
||||
def test_instructions(given, expected):
|
||||
assert process_instructions(given) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('given, expected',
|
||||
[
|
||||
('^v', 3)
|
||||
])
|
||||
def test_part2_instructions(given, expected):
|
||||
assert process_robo_instructions(given) == expected
|
||||
Reference in New Issue
Block a user