Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
38
Task/24-game-Solve/Jq/24-game-solve-1.jq
Normal file
38
Task/24-game-Solve/Jq/24-game-solve-1.jq
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
# Generate a stream of the permutations of the input array.
|
||||
def permutations:
|
||||
if length == 0 then []
|
||||
else range(0;length) as $i
|
||||
| [.[$i]] + (del(.[$i])|permutations)
|
||||
end ;
|
||||
|
||||
# Generate a stream of arrays of length n,
|
||||
# with members drawn from the input array.
|
||||
def take(n):
|
||||
length as $l |
|
||||
if n == 1 then range(0;$l) as $i | [ .[$i] ]
|
||||
else take(n-1) + take(1)
|
||||
end;
|
||||
|
||||
# Emit an array with elements that alternate between those in the input array and those in short,
|
||||
# starting with the former, and using nothing if "short" is too too short.
|
||||
def intersperse(short):
|
||||
. as $in
|
||||
| reduce range(0;length) as $i
|
||||
([]; . + [ $in[$i], (short[$i] // empty) ]);
|
||||
|
||||
# Emit a stream of all the nested triplet groupings of the input array elements,
|
||||
# e.g. [1,2,3,4,5] =>
|
||||
# [1,2,[3,4,5]]
|
||||
# [[1,2,3],4,5]
|
||||
#
|
||||
def triples:
|
||||
. as $in
|
||||
| if length == 3 then .
|
||||
elif length == 1 then $in[0]
|
||||
elif length < 3 then empty
|
||||
else
|
||||
(range(0; (length-1) / 2) * 2 + 1) as $i
|
||||
| ($in[0:$i] | triples) as $head
|
||||
| ($in[$i+1:] | triples) as $tail
|
||||
| [$head, $in[$i], $tail]
|
||||
end;
|
||||
23
Task/24-game-Solve/Jq/24-game-solve-2.jq
Normal file
23
Task/24-game-Solve/Jq/24-game-solve-2.jq
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
# Evaluate the input, which must be a number or a triple: [x, op, y]
|
||||
def eval:
|
||||
if type == "array" then
|
||||
.[1] as $op
|
||||
| if .[0] == null or .[2] == null then null
|
||||
else
|
||||
(.[0] | eval) as $left | (.[2] | eval) as $right
|
||||
| if $left == null or $right == null then null
|
||||
elif $op == "+" then $left + $right
|
||||
elif $op == "-" then $left - $right
|
||||
elif $op == "*" then $left * $right
|
||||
elif $op == "/" then
|
||||
if $right == 0 then null
|
||||
else $left / $right
|
||||
end
|
||||
else "invalid arithmetic operator: \($op)" | error
|
||||
end
|
||||
end
|
||||
else .
|
||||
end;
|
||||
|
||||
def pp:
|
||||
"\(.)" | explode | map([.] | implode | if . == "," then " " elif . == "\"" then "" else . end) | join("");
|
||||
21
Task/24-game-Solve/Jq/24-game-solve-3.jq
Normal file
21
Task/24-game-Solve/Jq/24-game-solve-3.jq
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
def OPERATORS: ["+", "-", "*", "/"];
|
||||
|
||||
# Input: an array of 4 digits
|
||||
# o: an array of 3 operators
|
||||
# Output: a stream
|
||||
def EXPRESSIONS(o):
|
||||
intersperse( o ) | triples;
|
||||
|
||||
def solve(objective):
|
||||
length as $length
|
||||
| [ (OPERATORS | take($length-1)) as $poperators
|
||||
| permutations | EXPRESSIONS($poperators)
|
||||
| select( eval == objective)
|
||||
] as $answers
|
||||
| if $answers|length > 3 then "That was too easy. I found \($answers|length) answers, e.g. \($answers[0] | pp)"
|
||||
elif $answers|length > 1 then $answers[] | pp
|
||||
else "You lose! There are no solutions."
|
||||
end
|
||||
;
|
||||
|
||||
solve(24), "Please try again."
|
||||
16
Task/24-game-Solve/Jq/24-game-solve-4.jq
Normal file
16
Task/24-game-Solve/Jq/24-game-solve-4.jq
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
$ jq -r -f Solve.jq
|
||||
[1,2,3,4]
|
||||
That was too easy. I found 242 answers, e.g. [4 * [1 + [2 + 3]]]
|
||||
Please try again.
|
||||
[1,2,3,40,1]
|
||||
That was too easy. I found 636 answers, e.g. [[[1 / 2] * 40] + [3 + 1]]
|
||||
Please try again.
|
||||
[3,8,9]
|
||||
That was too easy. I found 8 answers, e.g. [[8 / 3] * 9]
|
||||
Please try again.
|
||||
[4,5,6]
|
||||
You lose! There are no solutions.
|
||||
Please try again.
|
||||
[1,2,3,4,5,6]
|
||||
That was too easy. I found 197926 answers, e.g. [[2 * [1 + 4]] + [3 + [5 + 6]]]
|
||||
Please try again.
|
||||
Loading…
Add table
Add a link
Reference in a new issue