Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
22
Task/Zebra-puzzle/Jq/zebra-puzzle-1.jq
Normal file
22
Task/Zebra-puzzle/Jq/zebra-puzzle-1.jq
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# Attempt to unify the input object with the specified object
|
||||
def unify( object ):
|
||||
# Attempt to unify the input object with the specified tag:value
|
||||
def unify2(tag; value):
|
||||
if . == null then null
|
||||
elif .[tag] == value then .
|
||||
elif .[tag] == null then .[tag] = value
|
||||
else null
|
||||
end;
|
||||
reduce (object|keys[]) as $key
|
||||
(.; unify2($key; object[$key]) );
|
||||
|
||||
# Input: an array
|
||||
# Output: if the i-th element can be made to satisfy the condition,
|
||||
# then the updated array, otherwise empty.
|
||||
def enforce(i; cond):
|
||||
if 0 <= i and i < length
|
||||
then
|
||||
(.[i] | cond) as $ans
|
||||
| if $ans then .[i] = $ans else empty end
|
||||
else empty
|
||||
end ;
|
||||
60
Task/Zebra-puzzle/Jq/zebra-puzzle-2.jq
Normal file
60
Task/Zebra-puzzle/Jq/zebra-puzzle-2.jq
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
# Each house is a JSON object of the form:
|
||||
# { "number": _, "nation": _, "owns": _, "color": _, "drinks": _, "smokes": _}
|
||||
|
||||
# The list of houses is represented by an array of five such objects.
|
||||
|
||||
# Input: an array of objects representing houses.
|
||||
# Output: [i, solution] where i is the entity unified with obj
|
||||
# and solution is the updated array
|
||||
def solve_with_index( obj ):
|
||||
. as $Houses
|
||||
| range(0; length) as $i
|
||||
| ($Houses[$i] | unify(obj)) as $H
|
||||
| if $H then $Houses[$i] = $H else empty end
|
||||
| [ $i, .] ;
|
||||
|
||||
def solve( object ):
|
||||
solve_with_index( object )[1];
|
||||
|
||||
def adjacent( obj1; obj2 ):
|
||||
solve_with_index(obj1) as $H
|
||||
| $H[1]
|
||||
| (enforce( $H[0] - 1; unify(obj2) ),
|
||||
enforce( $H[0] + 1; unify(obj2) )) ;
|
||||
|
||||
def left_right( obj1; obj2 ):
|
||||
solve_with_index(obj1) as $H
|
||||
| $H[1]
|
||||
| enforce( $H[0] + 1; unify(obj2) ) ;
|
||||
|
||||
|
||||
# All solutions by generate-and-test
|
||||
def zebra:
|
||||
[range(0;5)] | map({"number": .}) # Five houses
|
||||
|
||||
| enforce( 0; unify( {"nation": "norwegian"} ) )
|
||||
| enforce( 2; unify( {"drinks": "milk"} ) )
|
||||
|
||||
| solve( {"nation": "englishman", "color": "red"} )
|
||||
| solve( {"nation": "swede", "owns": "dog"} )
|
||||
| solve( {"nation": "dane", "drinks": "tea"} )
|
||||
|
||||
| left_right( {"color": "green"}; {"color": "white"})
|
||||
|
||||
| solve( {"drinks": "coffee", "color": "green"} )
|
||||
| solve( {"smokes": "Pall Mall", "owns": "birds"} )
|
||||
| solve( {"color": "yellow", "smokes": "Dunhill"} )
|
||||
|
||||
| adjacent( {"smokes": "Blend" }; {"owns": "cats"} )
|
||||
| adjacent( {"owns": "horse"}; {"smokes": "Dunhill"})
|
||||
|
||||
| solve( {"drinks": "beer", "smokes": "Blue Master"} )
|
||||
| solve( {"nation": "german", "smokes": "Prince"})
|
||||
|
||||
| adjacent( {"nation": "norwegian"}; {"color": "blue"})
|
||||
| adjacent( {"drinks": "water"}; {"smokes": "Blend"})
|
||||
|
||||
| solve( {"owns": "zebra"} )
|
||||
;
|
||||
|
||||
zebra
|
||||
48
Task/Zebra-puzzle/Jq/zebra-puzzle-3.jq
Normal file
48
Task/Zebra-puzzle/Jq/zebra-puzzle-3.jq
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
$ time jq -n -f zebra.jq
|
||||
[
|
||||
{
|
||||
"number": 0,
|
||||
"nation": "norwegian",
|
||||
"color": "yellow",
|
||||
"smokes": "Dunhill",
|
||||
"owns": "cats",
|
||||
"drinks": "water"
|
||||
},
|
||||
{
|
||||
"number": 1,
|
||||
"drinks": "tea",
|
||||
"nation": "dane",
|
||||
"smokes": "Blend",
|
||||
"owns": "horse",
|
||||
"color": "blue"
|
||||
},
|
||||
{
|
||||
"number": 2,
|
||||
"drinks": "milk",
|
||||
"color": "red",
|
||||
"nation": "englishman",
|
||||
"owns": "birds",
|
||||
"smokes": "Pall Mall"
|
||||
},
|
||||
{
|
||||
"number": 3,
|
||||
"color": "green",
|
||||
"drinks": "coffee",
|
||||
"nation": "german",
|
||||
"smokes": "Prince",
|
||||
"owns": "zebra"
|
||||
},
|
||||
{
|
||||
"number": 4,
|
||||
"nation": "swede",
|
||||
"owns": "dog",
|
||||
"color": "white",
|
||||
"drinks": "beer",
|
||||
"smokes": "Blue Master"
|
||||
}
|
||||
]
|
||||
|
||||
# Times include compilation:
|
||||
real 0m0.284s
|
||||
user 0m0.260s
|
||||
sys 0m0.005s
|
||||
Loading…
Add table
Add a link
Reference in a new issue