algorithms

Algorithm implementations
git clone git://git.laack.co/algorithms.git
Log | Files | Refs | README

aoc_2025_1.py (1145B)


      1 # 0 - 99
      2 
      3 class Dial:
      4     
      5     position = 0
      6     
      7     def __init__(self, starting_position):
      8         self.position = starting_position
      9     def rotate_right(self, num):
     10             self.position = (self.position + num) % 100
     11     def rotate_left(self, num):
     12             self.position = (self.position - num)
     13             while self.position < 0:
     14                 self.position += 100
     15     
     16     # returns number of times the dial was at 0
     17     # not including starting state
     18     
     19     def follow_instructions(self, instructions):
     20         count = 0
     21         for instruction in instructions:
     22             if len(instruction) < 2:
     23                 raise ValueError()
     24                 
     25             if instruction[0] == 'L':
     26                 self.rotate_left(int(instruction[1:]))
     27             elif instruction[0] == 'R':
     28                 self.rotate_right(int(instruction[1:]))
     29             else:
     30                 raise ValueError()
     31             if self.position == 0:
     32                 count += 1
     33         return count
     34 
     35 dial = Dial(50)
     36 
     37 instructions = [
     38 "L68",
     39 "L30",
     40 "R48",
     41 "L5",
     42 "R60",
     43 "L55",
     44 "L1",
     45 "L99",
     46 "R14",
     47 "L82"
     48 ]
     49 
     50 print(dial.follow_instructions(instructions))