Talk:Railway circuit: Difference between revisions

→‎All solutions are wrong: short example code
(→‎All solutions are wrong: short example code)
Line 241:
::::::: A right handed fur-lined glove can have a duplicate by rotation that is an exact duplicate. But it is not an exact duplicate of a left handed fur-lined glove, is it? I do understand your reading.--[[User:Wherrera|Wherrera]] ([[User talk:Wherrera|talk]]) 23:13, 5 July 2022 (UTC)
:::::::: You are confusing "A becomes the same as B after reflection" with "A remains the same after reflection". A left handed glove looks like a right handed one in a mirror, no? You seem to suggest the task wants us to remove each solution that's its own mirror image, otherwise what's the distinction between "reflexion" and "rotation" in the wording in your opinion? --[[User:Katzenbaer|Katzenbaer]] ([[User talk:Katzenbaer|talk]]) 04:24, 6 July 2022 (UTC)
 
Minimum example code showing the correct math for validity check. This code doesn't filter out duplicates and is of course horribly slow.
<lang python>from itertools import product, accumulate
from cmath import pi, exp
 
N = 12 # defines size of each turn; 12 means 30 degrees, but other turn angles work, too
ANGLE = 2*pi/N
 
# 1: right; 0: straight; -1: left. Generates all combinations of them at given length.
sequences = lambda n: product(*([(1, 0, -1)]*n))
 
def valid(s):
# a is the right turn count after each step
a = tuple(accumulate(s))
# "< 1e-6" really should have been "== 0" but for numerical precision
return a[-1]%N == 0 and abs(sum(exp(x*1j*ANGLE) for x in a)) < 1e-6
 
for n in [12, 14]:
print(f'{n} tracks:')
for s in sequences(n):
if valid(s):
print(s)</lang>