Railway circuit: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
m (→{{header|Wren}}: Changed to Wren S/H) |
|||
(One intermediate revision by one other user not shown) | |||
Line 41:
Count the number of circuits '''Cn,m''' with '''n''' same as above and '''m''' = 2 to 8
<br><br>
=={{header|11l}}==
{{trans|Kotlin}}
<syntaxhighlight lang="11l">
-V
RIGHT = 1
LEFT = -1
STRAIGHT = 0
F normalize(tracks)
V a = tracks.map(tr -> ‘abc’[tr + 1]).join(‘’)
V norm = a
L 0 .< tracks.len
I a < norm
norm = a
a = a[1..]‘’a[0]
R norm
F full_circle_straight(tracks, nStraight)
I nStraight == 0
R 1B
I sum(tracks.map(i -> Int(i == :STRAIGHT))) != nStraight
R 0B
V straight = [0] * 12
V i = 0
V idx = 0
L i < tracks.len & idx >= 0
I tracks[i] == :STRAIGHT
straight[idx % 12]++
idx += tracks[i]
i++
R !(any((0.<6).map(i -> @straight[i] != @straight[i + 6]))
& any((0.<8).map(i -> @straight[i] != @straight[i + 4])))
F full_circle_right(tracks)
I sum(tracks) * 30 % 360 != 0
R 0B
V rTurns = [0] * 12
V i = 0
V idx = 0
L i < tracks.len & idx >= 0
I tracks[i] == :RIGHT
rTurns[idx % 12]++
idx += tracks[i]
i++
R !(any((0.<6).map(i -> @rTurns[i] != @rTurns[i + 6]))
& any((0.<8).map(i -> @rTurns[i] != @rTurns[i + 4])))
T PermutationsGen
[Int] indices, choices
carry = 0
F (numPositions, choices)
.indices = [0] * numPositions
.choices = copy(choices)
F next()
.carry = 1
V i = 1
L i < .indices.len & .carry > 0
.indices[i] += .carry
.carry = 0
I .indices[i] == .choices.len
.carry = 1
.indices[i] = 0
i++
R .indices.map(i -> @.choices[i])
F has_next()
R .carry != 1
F get_permutations_gen(nCurved, nStraight)
assert((nCurved + nStraight - 12) % 4 == 0, ‘input must be 12 + k * 4’)
V trackTypes = [:RIGHT, :LEFT]
I nStraight != 0
I nCurved == 12
trackTypes = [:RIGHT, :STRAIGHT]
E
trackTypes = [:RIGHT, :LEFT, :STRAIGHT]
R PermutationsGen(nCurved + nStraight, trackTypes)
F report(sol, numC, numS)
print("\n#. solution(s) for C#.,#. ".format(sol.len, numC, numS))
I numC <= 20
L(tracks) sorted(sol.values())
L(track) tracks
print(‘#2 ’.format(track), end' ‘’)
print()
F circuits(nCurved, nStraight)
[String = [Int]] solutions
V gen = get_permutations_gen(nCurved, nStraight)
L gen.has_next()
V tracks = gen.next()
I !full_circle_straight(tracks, nStraight)
L.continue
I !full_circle_right(tracks)
L.continue
solutions[normalize(tracks)] = tracks
report(solutions, nCurved, nStraight)
L(n) (12..24).step(4)
circuits(n, 0)
circuits(12, 4)
</syntaxhighlight>
{{out}}
<pre>
1 solution(s) for C12,0
1 1 1 1 1 1 1 1 1 1 1 1
1 solution(s) for C16,0
1 1 1 1 1 1 1 -1 1 1 1 1 1 1 1 -1
6 solution(s) for C20,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 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
40 solution(s) for C24,0
4 solution(s) for C12,4
1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0
1 1 1 1 0 1 1 0 1 1 1 1 0 1 1 0
1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 0
1 1 1 1 1 1 0 0 1 1 1 1 1 1 0 0
</pre>
=={{header|EchoLisp}}==
Line 2,465 ⟶ 2,610:
A tough task for the Wren interpreter requiring about 7 minutes 48 seconds to run. Unlike the Julia example, I've not attempted the straight track for N = 16 (which I estimate would take about an hour in isolation) and have not tried to go beyond N = 24.
<syntaxhighlight lang="
import "./math" for Int, Nums
|