Railway circuit: Difference between revisions

julia example
(julia example)
Line 589:
1 1 1 1 1 1 0 0 1 1 1 1 1 1 0 0
1 1 1 1 0 1 1 0 1 1 1 1 0 1 1 0
</pre>
 
=={{header|Julia}}==
<lang julia>import Main.≈, Main.+
 
struct Point{T}
x::T
y::T
end
+(p::Point, q) = Point(p.x + q.x, p.y + q.y)
≈(p::Point, q) = isapprox(p.x, q.x, atol=0.0001) && isapprox(p.y, q.y, atol=0.0001)
 
const twelvesteps = [Point(sinpi(a/6), cospi(a/6)) for a in 1:12]
const foursteps = [Point(sinpi(a/2), cospi(a/2)) for a in 1:4]
 
function addsymmetries!(infound, turns)
circularsymmetries(c) = [circshift(c, i) for i in 0:length(c)-1]
allsym = [circularsymmetries(turns); circularsymmetries([-x for x in turns])]
for c in allsym
infound[c] = 1
end
return maximum(allsym)
end
 
function isclosedpath(turns, straight, start=Point(0.0, 0.0))
if sum(turns) % (straight ? 4 : 12) != 0
return false
end
angl, point = 0, start
if straight
for turn in turns
angl += turn
point += foursteps[mod1(angl, 4)]
end
else
for turn in turns
angl += turn
point += twelvesteps[mod1(angl, 12)]
end
end
return point ≈ start
end
 
function allvalidcircuits(N, doprint, straight=false)
found = Vector{Vector{Int}}()
infound = Dict{Vector{Int},Int}()
println("\nFor N of $N and ", straight ? "straight" : "curved", " track: ")
for i in (straight ? (0:3^N-1) : (0:2^N-1))
turns = straight ?
[d == 0 ? 0 : (d == 1 ? -1 : 1) for d in digits(i, base=3, pad=N)] :
[d == 0 ? -1 : 1 for d in digits(i, base=2, pad=N)]
if isclosedpath(turns, straight) && !haskey(infound, turns)
canon = addsymmetries!(infound, turns)
doprint && println(canon)
push!(found, canon)
end
end
println("There are ", length(found), " unique valid circuits.")
return found
end
 
for i in 12:4:36
allvalidcircuits(i, i < 28)
end
 
for i in 4:2:16
allvalidcircuits(i, i < 12, true)
end
</lang>{{out}}
<pre>
For N of 12 and curved track:
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
There are 1 unique valid circuits.
 
For N of 16 and curved track:
[1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1]
There are 1 unique valid circuits.
 
For N of 20 and curved track:
[1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1]
[1, 1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1]
[1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1, 1, -1]
[1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, 1, -1]
[1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, 1, 1, -1]
[1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1]
There are 6 unique valid circuits.
 
For N of 24 and curved track:
There are 40 unique valid circuits.
 
For N of 28 and curved track:
There are 293 unique valid circuits.
 
For N of 32 and curved track:
There are 2793 unique valid circuits.
 
For N of 36 and curved track:
There are 30117 unique valid circuits.
 
For N of 4 and straight track:
[1, 1, 1, 1]
There are 1 unique valid circuits.
 
For N of 6 and straight track:
[1, 1, 0, 1, 1, 0]
There are 1 unique valid circuits.
 
For N of 8 and straight track:
[1, 1, 0, 0, 1, 1, 0, 0]
[1, 0, 1, 0, 1, 0, 1, 0]
[1, 1, 0, 1, 0, 1, 1, -1]
[1, 1, 1, 0, -1, -1, -1, 0]
[1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, -1, -1, -1, -1]
[1, 1, 1, -1, 1, 1, 1, -1]
There are 7 unique valid circuits.
 
For N of 10 and straight track:
[1, 1, 0, 0, 0, 1, 1, 0, 0, 0]
[1, 0, 1, 0, 0, 1, 0, 1, 0, 0]
[1, 1, -1, 1, 0, 1, 0, 1, 0, 0]
[1, 1, 0, 1, 0, 0, 1, 1, 0, -1]
[1, 1, 1, 0, -1, 0, -1, -1, 0, 0]
[1, 1, 0, 0, 1, 0, 1, 1, -1, 0]
[1, 1, 1, 0, 0, -1, -1, 0, -1, 0]
[1, 1, 1, 0, 0, -1, -1, -1, 1, -1]
[1, 1, 1, 0, 0, 1, 1, 1, -1, -1]
[1, 1, 0, 0, 1, 0, 1, 0, 1, -1]
[1, 1, 0, 0, 1, 1, -1, 1, 1, -1]
[1, 1, 1, 1, 1, 1, 0, 1, 1, 0]
[1, 1, 1, 1, -1, -1, 0, -1, -1, 0]
[1, 1, 1, -1, 1, 0, 1, 1, 0, -1]
[1, 1, 1, 1, -1, 0, -1, -1, 0, -1]
[1, 1, 1, -1, 0, 1, 1, 0, 1, -1]
[1, 1, 1, 1, 0, -1, -1, 0, -1, -1]
[1, 1, 1, 0, -1, 1, 1, 1, 0, -1]
[1, 1, 1, 0, 1, -1, -1, -1, 0, -1]
[1, 1, 0, 1, -1, 1, 1, 0, 1, -1]
[1, 1, -1, 1, 0, 1, 1, -1, 1, 0]
[1, 1, 1, -1, 0, 1, 1, 1, -1, 0]
[1, 1, 1, -1, 0, -1, -1, -1, 1, 0]
There are 23 unique valid circuits.
 
For N of 12 and straight track:
There are 141 unique valid circuits.
 
For N of 14 and straight track:
There are 871 unique valid circuits.
 
For N of 16 and straight track:
There are 6045 unique valid circuits.
</pre>
 
4,102

edits