day 1
This commit is contained in:
57
day-1.py
Normal file
57
day-1.py
Normal file
@@ -0,0 +1,57 @@
|
||||
|
||||
class SafeBreaker:
|
||||
def __init__(self):
|
||||
self.value = 50
|
||||
self.times_pointing_zero = 0
|
||||
|
||||
def move_dial(self, instruction: str) -> None:
|
||||
direction = instruction[0]
|
||||
multiplier = -1 if direction == "L" else 1
|
||||
|
||||
movement = int(instruction[1:])
|
||||
self.times_pointing_zero += int(movement / 100)
|
||||
|
||||
movement_value = multiplier * (movement % 100)
|
||||
|
||||
# Move
|
||||
original_value = self.value
|
||||
self.value = self.value + movement_value
|
||||
|
||||
if self.value < 0:
|
||||
if original_value != 0:
|
||||
self.times_pointing_zero += 1
|
||||
print("past 0")
|
||||
self.value = 100 + self.value
|
||||
elif self.value >= 100:
|
||||
print("past 0")
|
||||
self.times_pointing_zero += 1
|
||||
self.value = self.value % 100
|
||||
elif self.value == 0:
|
||||
self.times_pointing_zero += 1
|
||||
|
||||
|
||||
def get_password(self) -> int:
|
||||
return self.times_pointing_zero
|
||||
|
||||
def crack_safe(self, instructions: list[str]) -> int:
|
||||
for instruction in instructions:
|
||||
self.move_dial(instruction)
|
||||
print(instruction, self.value)
|
||||
|
||||
return self.get_password()
|
||||
|
||||
if __name__ == "__main__":
|
||||
instructions = []
|
||||
|
||||
with open("./day-1.txt", "r") as file:
|
||||
for line in file.readlines():
|
||||
instructions.append(line.strip())
|
||||
|
||||
sb = SafeBreaker()
|
||||
password = sb.crack_safe(instructions)
|
||||
|
||||
print(password)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user