[Python] Python - Itertools and Simple Loops[Python] Python - Itertools and Simple Loops
⏲️
Broken Clocks
Week 24, 2026
"""
THIS QUESTION WAS QUITE THE HELL TO READ...
\/
[]>_<]
| |--
/ \
drew this silly guy to relax lol
"""
from itertools import combinations
class clock:
def __init__(self, incrementValue: int = 0):
self._hh = 0
self._mm = 0
self._ss = 0
self._overAllSeconds = 0
self._incrementValueSeconds = incrementValue
def increment_clock(self) -> None:
self._overAllSeconds += self._incrementValueSeconds
self._hh, remainder = divmod(self._overAllSeconds, 3600)
self._mm, self._ss = divmod(remainder, 60)
def getOverAllSeconds(self) -> int:
return self._overAllSeconds
def __str__(self):
return f"{self._hh}:{self._mm:02}:{self._ss:02} | + {self._incrementValueSeconds} | ttl: {self._overAllSeconds} s"
def checkSync(clocks: list[clock]) -> bool:
return all(c.getOverAllSeconds() % 60 == 0 for c in clocks)
def updateClocks(clocks: list[clock]) -> None:
for c in clocks: c.increment_clock()
def findSyncTime(incorrect_speeds: tuple[int, ...]) -> int:
CLOCKS = [clock(incrementValue=60)] + [clock(incrementValue=s) for s in incorrect_speeds]
while True:
updateClocks(CLOCKS)
if checkSync(CLOCKS):
return CLOCKS[0].getOverAllSeconds()
# main
SPEEDS = list(range(1, 61)) # 1 to 59 (60 is the correct clock)
# 2-clock combos (1 correct + 2 incorrect)
results_2 = {}
for combo in combinations(SPEEDS, 2):
t = findSyncTime(combo)
results_2.setdefault(t, []).append(combo)
max_2 = max(results_2)
best_2 = results_2[max_2]
# 3-clock combos (1 correct + 3 incorrect)
results_3 = {}
for combo in combinations(SPEEDS, 3):
t = findSyncTime(combo)
results_3.setdefault(t, []).append(combo)
max_3 = max(results_3)
best_3 = results_3[max_3]
# SAVE TO FILE
with open("ans.txt", "w") as f:
for combo in best_2: f.write(f"{' '.join(map(str, combo))}\n")
f.write(f"{len(best_2)} unique 2-clock combinations took {max_2}s to sync to a whole minute\n")
for combo in best_3: f.write(f"{' '.join(map(str, combo))}\n")
f.write(f"{len(best_3)} unique 3-clock combinations took {max_3}s to sync to a whole minute\n")