Railway circuit: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(5 intermediate revisions by 3 users 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}}==
<langsyntaxhighlight lang="scheme">
;; R is turn counter in right direction
;; The nb of right turns in direction i
Line 146 ⟶ 291:
(printf "Number of circuits C%d,%d : %d" maxn straight (vector-length *circuits*)))
(when (< (vector-length *circuits*) 20) (for-each writeln *circuits*)))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 183 ⟶ 328:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 382 ⟶ 527:
}
circuits(12, 4)
}</langsyntaxhighlight>
 
{{out}}
Line 414 ⟶ 559:
 
=={{header|J}}==
Implementation (could be further optimized0:<langsyntaxhighlight Jlang="j">NB. is circuit valid?
vrwc=: {{ */1=({:,1++/)*/\^j.1r6p1*y }}"1
 
Line 440 ⟶ 585:
end.
'SLR'{~~.r
}}</langsyntaxhighlight>
 
Task examples:<langsyntaxhighlight Jlang="j"> rwc 12
LLLLLLLLLLLL
rwc 16
Line 463 ⟶ 608:
LLLLLSLSLLLLLSLS
LLLLSLLSLLLLSLLS
LLLSLLLSLLLSLLLS</langsyntaxhighlight>
 
Symbols:
Line 472 ⟶ 617:
=={{header|Java}}==
{{works with|Java|8}}
<langsyntaxhighlight lang="java">package railwaycircuit;
 
import static java.util.Arrays.stream;
Line 633 ⟶ 778:
return carry != 1;
}
}</langsyntaxhighlight>
<pre>1 solution(s) for C12,0
1 1 1 1 1 1 1 1 1 1 1 1
Line 664 ⟶ 809:
 
=={{header|Julia}}==
[[File:Railway_circuit_examples.png|320px]]
<lang ruby>
<syntaxhighlight lang="julia">
#=
Exhaustive search for complete railway (railroad track) circuits. A valid circuit begins and ends at the
same point faciing in the same direction. Track are either right handed or left handed 30 degree
Line 694 ⟶ 841:
 
Graphic displays of solutions are via a Gtk app and Cairo graphics.
=#
""" Rosetta Code task rosettacode.org/wiki/Railway_circuit. """
 
Line 900 ⟶ 1,048:
@time allvalidcircuits(i; verbose = !str && i < 28, reversals = rev, straight = str, graphic = i == 24)
end
</langsyntaxhighlight>{{out}}
<pre>
For N of 4 and curved track, including reversed curves:
Line 1,285 ⟶ 1,433:
{{trans|Java}}
It takes several minutes to get up to n = 32. I called it a day after that!
<langsyntaxhighlight lang="scala">// Version 1.2.31
 
const val RIGHT = 1
Line 1,412 ⟶ 1,560:
for (n in 12..32 step 4) circuits(n, 0)
circuits(12, 4)
}</langsyntaxhighlight>
 
{{out}}
Line 1,450 ⟶ 1,598:
It took about 2 min 25 s to get the result for a maximum of 32 and 41 min 15 s for a maximum of 36.
 
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils, strformat, strutils, sugar, tables
 
type
Line 1,577 ⟶ 1,725:
for n in countup(12, 36, 4):
circuits(n, 0)
circuits(12, 4)</langsyntaxhighlight>
 
{{out}}
Line 1,611 ⟶ 1,759:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,681 ⟶ 1,829:
 
allvalidcircuits($_, True) for 12, 16, 20;
allvalidcircuits($_, True, True) for 4, 6, 8;</langsyntaxhighlight>
{{out}}
<pre>For N of 12 and curved track:
Line 1,720 ⟶ 1,868:
=={{header|Phix}}==
{{trans|Go}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">right</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span>
Line 1,871 ⟶ 2,019:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">circuits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,900 ⟶ 2,048:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from itertools import count, islice
import numpy as np
from numpy import sin, cos, pi
Line 2,046 ⟶ 2,194:
sols.append(s)
 
draw_all(sols[:40])</langsyntaxhighlight>
 
=={{header|Racket}}==
Line 2,053 ⟶ 2,201:
Made functional, so builds the track up with lists. A bit more expense spent copying vectors, but this solution avoids mutation (except internally in <code>vector+=</code> . Also got rid of the maximum workload counter.
 
<langsyntaxhighlight lang="racket">#lang racket
 
(define-syntax-rule (vector+= v idx i)
Line 2,123 ⟶ 2,271:
(gen 20)
(gen 24)
(gen 12 4))</langsyntaxhighlight>
 
{{out}}
Line 2,157 ⟶ 2,305:
 
{{trans|Julia}}
<syntaxhighlight lang="raku" perl6line>#!/usr/bin/env raku
 
# 20200406 Raku programming solution
Line 2,209 ⟶ 2,357:
 
allvalidcircuits($_, $_ < 28) for 12, 16, 20; # 12, 16 … 36
allvalidcircuits($_, $_ < 12, True) for 4, 6, 8; # 4, 6 … 16;</langsyntaxhighlight>
{{out}}
<pre>For N of 12 and curved track:
Line 2,250 ⟶ 2,398:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="swift">enum Track: Int, Hashable {
case left = -1, straight, right
}
Line 2,425 ⟶ 2,573:
}
 
circuits(nCurved: 12, nStraight: 4)</langsyntaxhighlight>
 
{{out}}
Line 2,462 ⟶ 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.
<langsyntaxhighlight ecmascriptlang="wren">import "./seq" for Lst
import "./math" for Int, Nums
 
Line 2,629 ⟶ 2,777:
}
n = n + 2
}</langsyntaxhighlight>
 
{{out}}
Line 2,832 ⟶ 2,980:
=={{header|zkl}}==
{{trans|EchoLisp}}
<langsyntaxhighlight lang="zkl"> // R is turn counter in right direction
// The nb of right turns in direction i
// must be = to nb of right turns in direction i+6 (opposite)
Line 2,909 ⟶ 3,057:
else println("Number of circuits C%,d,%d : %d".fmt(maxn,straight,_circuits.len()));
if(_circuits.len()<20) _circuits.apply2(T(T("toString",*),"println"));
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">gen(12); println();
gen(16); println();
gen(20); println();
gen(24); println();
gen(12,4);</langsyntaxhighlight>
{{out}}
<pre>
9,479

edits