advent_of_code/15/1/main.py
2025-12-02 07:55:08 -06:00

60 lines
1.4 KiB
Python

'''Day 1 of 2015 AOC'''
def process_input_string(input_string: str) -> int:
'''Take in an input string and give back the floor number based on the instructions.
Keyword Args:
input_string (str) - String of encoded instructions
Return:
int - Floor number
'''
curr_floor = 0
for char in input_string:
if char == '(':
curr_floor += 1
elif char == ')':
curr_floor -= 1
return curr_floor
def find_the_basement(input_string: str) -> int:
'''Take in an input string of instructions and give back the position of the instruction entering the basement, or hitting floor "-1".
Keyword Args:
input_string (str) - String of encoded instructions
Return:
int - Position of instruction in the string
'''
curr_floor = 0
for index, char in enumerate(input_string):
if char == '(':
curr_floor += 1
elif char == ')':
curr_floor -= 1
if curr_floor == -1:
# Add one to give the 1s based position instead of the 0s based index
return index + 1
def main():
'''Entrypoint'''
with open('input.txt', encoding='utf-8') as fh:
input_string = fh.readline()
output = process_input_string(input_string)
print(f'Part 1: {output}')
output = find_the_basement(input_string)
print(f'Part 2: {output}')
if __name__ == "__main__":
main()