45 lines
1015 B
Python
45 lines
1015 B
Python
|
|
|
|
def dial(action, start_position):
|
|
if action[0] == "R":
|
|
position = start_position + action[1]
|
|
else:
|
|
position = start_position - action[1]
|
|
|
|
# Count rotations over "0" (multiples of 100)
|
|
zeros = int(abs(position) / 100)
|
|
|
|
# Negative position, or exactly "0", caused one extra "0" hit
|
|
# Except when starting at 0!
|
|
if position <= 0 and start_position != 0:
|
|
zeros += 1
|
|
|
|
position = position % 100
|
|
|
|
return position, zeros
|
|
|
|
def read_input(filename):
|
|
actions = []
|
|
for line in open(filename):
|
|
line = line.strip()
|
|
|
|
direction = line[:1]
|
|
steps = int(line[1:])
|
|
actions.append((direction,steps))
|
|
return actions
|
|
|
|
|
|
# filename = "day01/my_example_input"
|
|
# filename = "day01/example_input" # Output is 6
|
|
filename = "day01/input" # Output is 6133
|
|
actions = read_input(filename)
|
|
|
|
position = 50
|
|
password = 0
|
|
|
|
for action in actions:
|
|
position, zeros = dial(action, position)
|
|
password += zeros
|
|
|
|
print(f"Password is {password}")
|