2025-12-02 07:55:08 -06:00

67 lines
1.8 KiB
Python

'''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()