Implement main logic for counting crossings and determining final position in the lock mechanism
This commit is contained in:
@@ -4033,4 +4033,4 @@ R17
|
|||||||
L19
|
L19
|
||||||
R42
|
R42
|
||||||
R48
|
R48
|
||||||
R39
|
R39
|
||||||
32
Day1/Part2/main.py
Normal file
32
Day1/Part2/main.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
def count_crossings_segment(P, delta):
|
||||||
|
P_new = P + delta
|
||||||
|
if delta > 0:
|
||||||
|
# positive Drehung: 100 * m in (P, P_new]
|
||||||
|
crossings = (P_new // 100) - (P // 100)
|
||||||
|
elif delta < 0:
|
||||||
|
# negative Drehung: 100 * m in [P_new, P-1]
|
||||||
|
crossings = ((P - 1) // 100) - ((P_new - 1) // 100)
|
||||||
|
else:
|
||||||
|
crossings = 0
|
||||||
|
return crossings, P_new
|
||||||
|
|
||||||
|
passes = 0
|
||||||
|
P = 50 # „entfaltete“ Position, nicht mod 100!
|
||||||
|
|
||||||
|
with open("input.txt", encoding="utf-8") as f:
|
||||||
|
for line in f:
|
||||||
|
line = line.strip()
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
|
||||||
|
d = line[0].upper()
|
||||||
|
steps = int(line[1:])
|
||||||
|
|
||||||
|
delta = steps if d == 'R' else -steps
|
||||||
|
c, P = count_crossings_segment(P, delta)
|
||||||
|
passes += c
|
||||||
|
|
||||||
|
final_pos = P % 100
|
||||||
|
|
||||||
|
print("Anzahl 0-PASSAGEN:", passes)
|
||||||
|
print("Endposition auf dem Schloss:", final_pos)
|
||||||
Reference in New Issue
Block a user