Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
36
Task/Wireworld/Nim/wireworld.nim
Normal file
36
Task/Wireworld/Nim/wireworld.nim
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import strutils, os
|
||||
|
||||
var world, world2 = """
|
||||
+-----------+
|
||||
|tH.........|
|
||||
|. . |
|
||||
| ... |
|
||||
|. . |
|
||||
|Ht.. ......|
|
||||
+-----------+"""
|
||||
let h = world.splitLines.len
|
||||
let w = world.splitLines[0].len
|
||||
|
||||
template isH(x, y): int = int(s[i+ w*y + x] == 'H')
|
||||
|
||||
proc next(o: var string, s: string, w: int) =
|
||||
for i, c in s:
|
||||
o[i] = case c
|
||||
of ' ': ' '
|
||||
of 't': '.'
|
||||
of 'H': 't'
|
||||
of '.':
|
||||
if (isH(-1, -1) + isH(0, -1) + isH(1, -1) +
|
||||
isH(-1, 0) + isH(1, 0) +
|
||||
isH(-1, 1) + isH(0, 1) + isH(1, 1)
|
||||
) in 1..2: 'H' else: '.'
|
||||
else: c
|
||||
|
||||
while true:
|
||||
echo world
|
||||
stdout.write "\x1b[",h,"A"
|
||||
stdout.write "\x1b[",w,"D"
|
||||
sleep 100
|
||||
|
||||
world2.next(world, w)
|
||||
swap world, world2
|
||||
31
Task/Wireworld/Sidef/wireworld.sidef
Normal file
31
Task/Wireworld/Sidef/wireworld.sidef
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
var f = [[], DATA.lines.map {['', .chars..., '']}..., []];
|
||||
|
||||
10.times {
|
||||
say f.map { .join(" ") + "\n" }.join;
|
||||
var a = [[]];
|
||||
for y in (1 .. f.end-1) {
|
||||
var r = f[y];
|
||||
var rr = [''];
|
||||
for x in (1 .. r.end-1) {
|
||||
var c = r[x];
|
||||
rr << (
|
||||
given(c) {
|
||||
when('H') { 't' }
|
||||
when('t') { '.' }
|
||||
when('.') { <. H>[f.ft(y-1, y+1).map{.ft(x-1, x+1)...}.count('H') ~~ [1,2]] }
|
||||
default { c }
|
||||
}
|
||||
)
|
||||
}
|
||||
rr << '';
|
||||
a << rr;
|
||||
}
|
||||
f = [a..., []];
|
||||
}
|
||||
|
||||
__DATA__
|
||||
tH.........
|
||||
. .
|
||||
...
|
||||
. .
|
||||
Ht.. ......
|
||||
37
Task/Wireworld/jq/wireworld-1.jq
Normal file
37
Task/Wireworld/jq/wireworld-1.jq
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
def lines: split("\n")|length;
|
||||
|
||||
def cols: split("\n")[0]|length + 1; # allow for the newline
|
||||
|
||||
# Is there an "H" at [x,y] relative to position i, assuming the width is w?
|
||||
# Input is an array; 72 is "H"
|
||||
def isH(x; y; i; w): if .[i+ w*y + x] == 72 then 1 else 0 end;
|
||||
|
||||
def neighborhood(i;w):
|
||||
isH(-1; -1; i; w) + isH(0; -1; i; w) + isH(1; -1; i; w) +
|
||||
isH(-1; 0; i; w) + isH(1; 0; i; w) +
|
||||
isH(-1; 1; i; w) + isH(0; 1; i; w) + isH(1; 1; i; w) ;
|
||||
|
||||
# The basic rules:
|
||||
# Input: a world
|
||||
# Output: the next state of .[i]
|
||||
def evolve(i; width) :
|
||||
# "Ht. " | explode => [ 72, 116, 46, 32 ]
|
||||
.[i] as $c
|
||||
| if $c == 32 then $c # " " => " "
|
||||
elif $c == 116 then 46 # "t" => "."
|
||||
elif $c == 72 then 116 # "H" => "t"
|
||||
elif $c == 46 then # "."
|
||||
# updates are "simultaneous" i.e. relative to $world
|
||||
neighborhood(i; width) as $sum
|
||||
| (if [1,2]|index($sum) then 72 else . end) # "H"
|
||||
else $c
|
||||
end ;
|
||||
|
||||
# [world, lines, cols] | next(w) => [world, lines, cols]
|
||||
def next:
|
||||
.[0] as $world | .[1] as $lines | .[2] as $w
|
||||
| reduce range(0; $world|length) as $i
|
||||
($world;
|
||||
$world | evolve($i; $w) as $next
|
||||
| if .[$i] == $next then . else .[$i] = $next end )
|
||||
| [., $lines, $w] ; #
|
||||
26
Task/Wireworld/jq/wireworld-2.jq
Normal file
26
Task/Wireworld/jq/wireworld-2.jq
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# "clear screen":
|
||||
def cls: "\u001b[2J";
|
||||
|
||||
# Input: an integer; 1000 ~ 1 sec
|
||||
def spin:
|
||||
reduce range(1; 500 * .) as $i
|
||||
(0; . + ($i|cos)*($i|cos) + ($i|sin)*($i|sin) )
|
||||
| "" ;
|
||||
|
||||
# Animate n steps;
|
||||
# if "sleep" is non-negative then cls and
|
||||
# sleep about "sleep" ms between frames.
|
||||
def animate(n; sleep):
|
||||
if n == 0 then empty
|
||||
else (if sleep >= 0 then cls else "" end),
|
||||
(.[0]|implode), n, "\n",
|
||||
(sleep|spin),
|
||||
( next|animate(n-1; sleep) )
|
||||
end ;
|
||||
|
||||
# Input: a string representing the initial state
|
||||
def animation(n; sleep):
|
||||
[ explode, lines, cols] | animate(n; sleep) ;
|
||||
|
||||
# Input: a string representing the initial state
|
||||
def frames(n): animation(n; -1);#
|
||||
15
Task/Wireworld/jq/wireworld-3.jq
Normal file
15
Task/Wireworld/jq/wireworld-3.jq
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
def world11:
|
||||
"+-----------+\n" +
|
||||
"|tH.........|\n" +
|
||||
"|. . |\n" +
|
||||
"| ... |\n" +
|
||||
"|. . |\n" +
|
||||
"|Ht.. ......|\n" +
|
||||
"+-----------+\n" ;
|
||||
|
||||
def world9:
|
||||
" \n" +
|
||||
" tH \n" +
|
||||
" . .... \n" +
|
||||
" .. \n" +
|
||||
" \n" ;
|
||||
2
Task/Wireworld/jq/wireworld-4.jq
Normal file
2
Task/Wireworld/jq/wireworld-4.jq
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# Ten-step animation with about 1 sec between frames
|
||||
world9 | animation(10; 1000)
|
||||
2
Task/Wireworld/jq/wireworld-5.jq
Normal file
2
Task/Wireworld/jq/wireworld-5.jq
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# Ten frames in sequence:
|
||||
world11 | frames(10)
|
||||
Loading…
Add table
Add a link
Reference in a new issue