from functools import lru_cache # +3;+6;*2 def m(h, last): if last and last == 3: return (h + 6, 6), (h * 2, 2) elif last and last == 6: return (h + 3, 3), (h * 2, 2) elif last and last == 2: return (h + 3, 3), (h + 6, 6) else: return (h + 3, 3), (h + 6, 6), (h * 2, 2) @lru_cache(None) def f(h, last): if h > 40: return 'END' if any(f(x[0], x[1]) == 'END' for x in m(h, last)): return 'P1' elif all(f(x[0], x[1]) == 'P1' for x in m(h, last)): return 'V1' elif any(f(x[0], x[1]) == 'V1' for x in m(h, last)): return 'P2' elif all(f(x[0], x[1]) in ['P1', 'P2'] for x in m(h, last)): return 'V2' for S in range(2, 36 + 1): res = f(S, 0) if res and res == 'V2': print(res, S) #20 #17 #6 8