Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,32 @@
constant txt = """
A B
/|\ /|\
/ | X | \
/ |/ \| \
C - D - E - F
\ |\ /| /
\ | X | /
\|/ \|/
G H"""
--constant links = "CA DA DB DC EA EB ED FB FE GC GD GE HD HE HF"
constant links = {"","","A","ABC","ABD","BE","CDE","DEF"}
function solve(sequence s, integer idx, sequence part)
object res
integer v, p
for i=1 to length(s) do
v = s[i]
for j=1 to length(links[idx]) do
p = links[idx][j]-'@'
if abs(v-part[p])<2 then v=0 exit end if
end for
if v then
if length(s)=1 then return part&v end if
res = solve(s[1..i-1]&s[i+1..$],idx+1,part&v)
if sequence(res) then return res end if
end if
end for
return 0
end function
printf(1,substitute_all(txt,"ABCDEFGH",solve("12345678",1,"")))

View file

@ -0,0 +1,24 @@
# Short-circuit determination of whether (a|condition)
# is true for all a in array:
def forall(array; condition):
def check:
. as $ix
| if $ix == (array|length) then true
elif (array[$ix] | condition) then ($ix + 1) | check
else false
end;
0 | check;
# permutations of 0 .. (n-1)
def permutations(n):
# Given a single array, generate a stream by inserting n at different positions:
def insert(m;n):
if m >= 0 then (.[0:m] + [n] + .[m:]), insert(m-1;n) else empty end;
if n==0 then []
elif n == 1 then [1]
else
permutations(n-1) | insert(n-1; n)
end;
# Count the number of items in a stream
def count(f): reduce f as $_ (0; .+1);

View file

@ -0,0 +1,13 @@
# Generate a stream of solutions.
# Input should be the connections array, i.e. an array of [i,j] pairs;
# N is the number of pegs and holds.
def solutions(N):
def abs: if . < 0 then -. else . end;
# Is the proposed permutation (the input) ok?
def ok(connections):
. as $p
| forall( connections;
(($p[.[0]] - $p[.[1]])|abs) != 1 );
. as $connections | permutations(N) | select(ok($connections);

View file

@ -0,0 +1,33 @@
# The connectedness matrix:
# In this table, 0 represents "A", etc, and an entry [i,j]
# signifies that the holes with indices i and j are connected.
def connections:
[[0, 2], [0, 3], [0, 4],
[1, 3], [1, 4], [1, 5],
[6, 2], [6, 3], [6, 4],
[7, 3], [7, 4], [7, 5],
[2, 3], [3, 4], [4, 5]]
;
def solve:
connections | solutions(8);
# pretty-print a solution for the 8-peg puzzle
def pp:
def pegs: ["A", "B", "C", "D", "E", "F", "G", "H"];
. as $in
| ("
A B
/|\\ /|\\
/ | X | \\
/ |/ \\| \\
C - D - E - F
\\ |\\ /| /
\\ | X | /
\\|/ \\|/
G H
" | explode) as $board
| (pegs | map(explode)) as $letters
| $letters
| reduce range(0;length) as $i ($board; index($letters[$i]) as $ix | .[$ix] = $in[$i] + 48)
| implode;

View file

@ -0,0 +1,12 @@
# To print all the solutions:
# solve | pp
# To count the number of solutions:
# count(solve)
# jq 1.4 lacks facilities for harnessing generators,
# but the following will suffice here:
def one(f): reduce f as $s
(null; if . == null then $s else . end);
one(solve) | pp

View file

@ -0,0 +1,11 @@
$ jq -n -r -f no_connection.jq
5 6
/|\ /|\
/ | X | \
/ |/ \| \
7 - 1 - 8 - 2
\ |\ /| /
\ | X | /
\|/ \|/
3 4