#!/bin/env python3 # 0-99 MAX_NUMBERS = 100 START_AT = 50 with open("example", "+r") as file: hit_zero = 0 position = START_AT for line in file.readlines(): # print(line) amount = int(line[1:]) direction = line[0] no_rotations = int(amount / MAX_NUMBERS) remaining = amount - no_rotations * MAX_NUMBERS position = position - remaining if direction == "L" else position + remaining if position < 0: position = MAX_NUMBERS + position if position >= MAX_NUMBERS: position = position - MAX_NUMBERS if position == 0: hit_zero = hit_zero + 1 print(f"The dial is rotated {direction}{amount} to point at {position}") print(f"hit 0 {hit_zero} times.") with open("input", "+r") as file: hit_zero = 0 position = START_AT for line in file.readlines(): previous_position = position amount = int(line[1:]) clockwise = True if line[0] == "R" else False rotations = int(amount / MAX_NUMBERS) remaining = amount - rotations * MAX_NUMBERS position = position + remaining if clockwise else position - remaining if (position <= 0 or position >= MAX_NUMBERS) and previous_position != 0: rotations = rotations + 1 if position < 0: position = MAX_NUMBERS + position if position >= MAX_NUMBERS: position = position - MAX_NUMBERS hit_zero = hit_zero + rotations print( f'dial rotated {rotations}x {"clockwise" if clockwise else "counter-clockwise"} from {previous_position} to {position} ({amount})' ) print(f"hit 0 {hit_zero} times.")