Intersecting number wheels: Difference between revisions

m (→‎{{header|Wren}}: Minor tidy)
 
(2 intermediate revisions by the same user not shown)
Line 1,334:
{"A":"1DD"},{"D":"678"}
{"A":"1BC"},{"B":"34"},{"C":"5B"}</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq'''
 
In this entry, a single wheel is simply represented by
a JSON object of the form { name: array }
 
where `name` is its name, and `array` is an array of the values on the wheel in the order
in which they would be read.
 
A set of of number of wheels can thus be represented simply as the sum of the objects corresponding to each wheel.
Thus the collection of illustrative number wheel groups can be defined as follows:
<syntaxhighlight lang="jq">
def wheels: [
{
"A": [1, 2, 3]
},
{
"A": [1, "B", 2],
"B": [3, 4]
},
{
"A": [1, "D", "D"],
"D": [6, 7, 8]
},
{
"A": [1, "B", "C"],
"B": [3, 4],
"C": [5, "B"]
}
];
</syntaxhighlight>
<syntaxhighlight lang="jq">
# read($wheel)
# where $wheel is the wheel to be read (a string)
# Input: a set of wheels
# Output: an object such that .value is the next value,
# and .state is the updated state of the set of wheels
def read($wheel):
 
# Input: an array
# Output: the rotated array
def rotate: .[1:] + [.[0]];
 
.[$wheel][0] as $value
| (.[$wheel] |= rotate) as $state
| if ($value | type) == "number"
then {$value, $state}
else $state | read($value)
end;
 
# Read wheel $wheel $n times
def multiread($wheel; $n):
if $n <= 0 then empty
else read($wheel)
| .value, (.state | multiread($wheel; $n - 1))
end;
 
def printWheels:
keys[] as $k
| "\($k): \(.[$k])";
 
# Spin each group $n times
def spin($n):
wheels[]
| "The number wheel group:",
printWheels,
"generates",
([ multiread("A"; $n) ] | join(" ") + " ..."),
"";
 
spin(20)
</syntaxhighlight>
'''Invocation'''
<pre>
jq -nr -f intersecting-number-wheels.jq
</pre>
 
{{output}}
<pre>
The number wheel group:
A: [1,2,3]
generates
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 ...
 
The number wheel group:
A: [1,"B",2]
B: [3,4]
generates
1 3 2 1 4 2 1 3 2 1 4 2 1 3 2 1 4 2 1 3 ...
 
The number wheel group:
A: [1,"D","D"]
D: [6,7,8]
generates
1 6 7 1 8 6 1 7 8 1 6 7 1 8 6 1 7 8 1 6 ...
 
The number wheel group:
A: [1,"B","C"]
B: [3,4]
C: [5,"B"]
generates
1 3 5 1 4 3 1 4 5 1 3 4 1 3 5 1 4 3 1 4 ...
</pre>
 
=={{header|Julia}}==
2,442

edits