60 lines
1.5 KiB
Python
60 lines
1.5 KiB
Python
'''AoC 2025 Day 1'''
|
|
import math
|
|
|
|
def turn_dial(curr_pos: int, instruction: str) -> int:
|
|
'''Turn dial from current position to the new one'''
|
|
direction, amount = process_instruction(instruction)
|
|
|
|
new_pos = (curr_pos + (direction * amount)) % 100
|
|
|
|
return new_pos
|
|
|
|
|
|
def check_cross_zero(curr_pos: int, instruction: str) -> int:
|
|
'''Check for if the instruction crosses 0 and lands on it'''
|
|
direction, amount = process_instruction(instruction)
|
|
|
|
total_zeros = 0
|
|
|
|
if direction == 1 and curr_pos + amount >= 100:
|
|
total_zeros += math.floor((curr_pos + amount) / 100)
|
|
if direction == -1 and curr_pos - amount <= 0:
|
|
total_zeros += math.floor(abs((curr_pos - amount) / 100))
|
|
if curr_pos != 0:
|
|
total_zeros += 1
|
|
|
|
return total_zeros
|
|
|
|
|
|
def process_instruction(instruction: str) -> (int, int):
|
|
'''Returns the direction and amount of the turn'''
|
|
direction = -1 if instruction[0] == 'L' else 1
|
|
amount = int(instruction[1:])
|
|
|
|
return direction, amount
|
|
|
|
|
|
def main():
|
|
'''Entrypoint'''
|
|
curr_pos = 50
|
|
|
|
num_zero = 0
|
|
crossing_zero = 0
|
|
|
|
with open('input.txt', encoding='utf-8') as fh:
|
|
for line in fh:
|
|
crossing_zero += check_cross_zero(curr_pos, line.strip())
|
|
|
|
curr_pos = turn_dial(curr_pos, line.strip())
|
|
|
|
if curr_pos == 0:
|
|
num_zero += 1
|
|
|
|
print(f'Part1: {num_zero}')
|
|
|
|
print(f'Part2: {crossing_zero}')
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|