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
|
|
@ -0,0 +1,48 @@
|
|||
PROGRAM DINESMAN
|
||||
|
||||
BEGIN
|
||||
! Floors are numbered 0 (ground) to 4 (top)
|
||||
|
||||
! "Baker, Cooper, Fletcher, Miller, and Smith live on different floors":
|
||||
stmt1$="Baker<>Cooper AND Baker<>Fletcher AND Baker<>Miller AND "+"Baker<>Smith AND Cooper<>Fletcher AND Cooper<>Miller AND "+"Cooper<>Smith AND Fletcher<>Miller AND Fletcher<>Smith AND "+"Miller<>Smith"
|
||||
|
||||
! "Baker does not live on the top floor":
|
||||
stmt2$="Baker<>4"
|
||||
|
||||
! "Cooper does not live on the bottom floor":
|
||||
stmt3$="Cooper<>0"
|
||||
|
||||
! "Fletcher does not live on either the top or the bottom floor":
|
||||
stmt4$="Fletcher<>0 AND Fletcher<>4"
|
||||
|
||||
! "Miller lives on a higher floor than does Cooper":
|
||||
stmt5$="Miller>Cooper"
|
||||
|
||||
! "Smith does not live on a floor adjacent to Fletcher's":
|
||||
stmt6$="ABS(Smith-Fletcher)<>1"
|
||||
|
||||
! "Fletcher does not live on a floor adjacent to Cooper's":
|
||||
stmt7$="ABS(Fletcher-Cooper)<>1"
|
||||
|
||||
FOR Baker=0 TO 4 DO
|
||||
FOR Cooper=0 TO 4 DO
|
||||
FOR Fletcher=0 TO 4 DO
|
||||
FOR Miller=0 TO 4 DO
|
||||
FOR Smith=0 TO 4 DO
|
||||
IF Baker<>4 AND Cooper<>0 AND Miller>Cooper THEN
|
||||
IF Fletcher<>0 AND Fletcher<>4 AND ABS(Smith-Fletcher)<>1 AND ABS(Fletcher-Cooper)<>1 THEN
|
||||
IF Baker<>Cooper AND Baker<>Fletcher AND Baker<>Miller AND Baker<>Smith AND Cooper<>Fletcher AND Cooper<>Miller AND Cooper<>Smith AND Fletcher<>Miller AND Fletcher<>Smith AND Miller<>Smith THEN
|
||||
PRINT("Baker lives on floor ";Baker)
|
||||
PRINT("Cooper lives on floor ";Cooper)
|
||||
PRINT("Fletcher lives on floor ";Fletcher)
|
||||
PRINT("Miller lives on floor ";Miller)
|
||||
PRINT("Smith lives on floor ";Smith)
|
||||
END IF
|
||||
END IF
|
||||
END IF
|
||||
END FOR ! Smith
|
||||
END FOR ! Miller
|
||||
END FOR ! Fletcher
|
||||
END FOR ! Cooper
|
||||
END FOR ! Baker
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
(require 'hash)
|
||||
(require' amb)
|
||||
|
||||
;;
|
||||
;; Solver
|
||||
;;
|
||||
|
||||
(define (dwelling-puzzle context names floors H)
|
||||
;; each amb calls gives a floor to a name
|
||||
(for ((name names))
|
||||
(hash-set H name (amb context floors)))
|
||||
;; They live on different floors.
|
||||
(amb-require (distinct? (amb-choices context)))
|
||||
(constraints floors H) ;; may fail and backtrack
|
||||
;; result returned to amb-run
|
||||
(for/list ((name names))
|
||||
(cons name (hash-ref H name)))
|
||||
;; (amb-fail) is possible here to see all solutions
|
||||
)
|
||||
|
||||
(define (task names)
|
||||
(amb-run dwelling-puzzle
|
||||
(amb-make-context)
|
||||
names
|
||||
(iota (length names)) ;; list of floors : 0,1, ....
|
||||
(make-hash)) ;; hash table : "name" -> floor
|
||||
)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
(define names '("baker" "cooper" "fletcher" "miller" "smith" ))
|
||||
|
||||
(define-syntax-rule (floor name) (hash-ref H name))
|
||||
(define-syntax-rule (touch a b) (= (abs (- (hash-ref H a) (hash-ref H b))) 1))
|
||||
|
||||
(define (constraints floors H)
|
||||
(define top (1- (length floors)))
|
||||
;; Baker does not live on the top floor.
|
||||
(amb-require (!= (floor "baker") top))
|
||||
;; Cooper does not live on the bottom floor.
|
||||
(amb-require (!= (floor "cooper") 0))
|
||||
;; Fletcher does not live on either the top or the bottom floor.
|
||||
(amb-require (!= (floor "fletcher") top))
|
||||
(amb-require (!= (floor "fletcher") 0))
|
||||
;; Miller lives on a higher floor than does Cooper.
|
||||
(amb-require (> (floor "miller") (floor "cooper")))
|
||||
;; Smith does not live on a floor adjacent to Fletcher's.
|
||||
(amb-require (not (touch "smith" "fletcher")))
|
||||
;; Fletcher does not live on a floor adjacent to Cooper's.
|
||||
(amb-require (not (touch "fletcher" "cooper")))
|
||||
)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(task names)
|
||||
→ ((baker . 2) (cooper . 1) (fletcher . 3) (miller . 4) (smith . 0))
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
;; add a name/floor
|
||||
(define names '("baker" "cooper" "fletcher" "miller" "smith" "antoinette"))
|
||||
|
||||
(define (constraints floors H)
|
||||
;; ... same as above, add the following
|
||||
|
||||
;; Antoinette does not like 💔 Smith
|
||||
(amb-require (not (touch "smith" "antoinette")))
|
||||
;; Antoinette is very close ❤️ to Cooper
|
||||
(amb-require (touch "cooper" "antoinette"))
|
||||
;; Antoinette wants a prime numbered floor
|
||||
(amb-require (prime? (floor "antoinette")))
|
||||
)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(task names)
|
||||
→ ((baker . 0) (cooper . 1) (fletcher . 3) (miller . 4) (smith . 5) (antoinette . 2))
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
floor1 = "return baker!=cooper and baker!=fletcher and baker!=miller and
|
||||
baker!=smith and cooper!=fletcher and cooper!=miller and
|
||||
cooper!=smith and fletcher!=miller and fletcher!=smith and
|
||||
miller!=smith"
|
||||
floor2 = "return baker!=4"
|
||||
floor3 = "return cooper!=0"
|
||||
floor4 = "return fletcher!=0 and fletcher!=4"
|
||||
floor5 = "return miller>cooper"
|
||||
floor6 = "return fabs(smith-fletcher)!=1"
|
||||
floor7 = "return fabs(fletcher-cooper)!=1"
|
||||
for baker = 0 to 4
|
||||
for cooper = 0 to 4
|
||||
for fletcher = 0 to 4
|
||||
for miller = 0 to 4
|
||||
for smith = 0 to 4
|
||||
if eval(floor2) if eval(floor3) if eval(floor5)
|
||||
if eval(floor4) if eval(floor6) if eval(floor7)
|
||||
if eval(floor1)
|
||||
see "baker lives on floor " + baker + nl
|
||||
see "cooper lives on floor " + cooper + nl
|
||||
see "fletcher lives on floor " + fletcher + nl
|
||||
see "miller lives on floor " + miller + nl
|
||||
see "smith lives on floor " + smith + nl ok ok ok ok ok ok ok
|
||||
next
|
||||
next
|
||||
next
|
||||
next
|
||||
next
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
func dinesman(problem) {
|
||||
var lines = problem.split('.');
|
||||
var names = lines.first.scan(/\b[A-Z]\w*/);
|
||||
var re_names = Regex(names.join('|'));
|
||||
|
||||
# Later on, search for these keywords (the word "not" is handled separately).
|
||||
var words = %w(first second third fourth fifth sixth seventh eighth ninth tenth
|
||||
bottom top higher lower adjacent);
|
||||
var re_keywords = Regex(words.join('|'));
|
||||
|
||||
# Build an array of lambda's
|
||||
var predicates = lines.ft(1, lines.end-1).map{ |line|
|
||||
var keywords = line.scan(re_keywords);
|
||||
var (name1, name2) = line.scan(re_names)...;
|
||||
|
||||
keywords.map{ |keyword|
|
||||
var l = do {
|
||||
given(keyword) {
|
||||
when ("bottom") { ->(c) { c.first == name1 } }
|
||||
when ("top") { ->(c) { c.last == name1 } }
|
||||
when ("higher") { ->(c) { c.index(name1) > c.index(name2) } }
|
||||
when ("lower") { ->(c) { c.index(name1) < c.index(name2) } }
|
||||
when ("adjacent") { ->(c) { c.index(name1) - c.index(name2) -> abs == 1 } }
|
||||
default { ->(c) { c[words.index(keyword)] == name1 } }
|
||||
}
|
||||
}
|
||||
line ~~ /\bnot\b/ ? func(c) { l(c) -> not } : l; # handle "not"
|
||||
}
|
||||
}.flatten;
|
||||
|
||||
names.permutations { |candidate|
|
||||
predicates.all { |predicate| predicate(candidate) } && return candidate;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
var demo1 = "Abe Ben Charlie David. Abe not second top. not adjacent Ben Charlie.
|
||||
David Abe adjacent. David adjacent Ben. Last line."
|
||||
|
||||
var demo2 = "A B C D. A not adjacent D. not B adjacent higher C. C lower D. Last line"
|
||||
|
||||
var problem1 = "Baker, Cooper, Fletcher, Miller, and Smith live on different floors of an apartment house that
|
||||
contains only five floors. Baker does not live on the top floor. Cooper does not live on the bottom floor.
|
||||
Fletcher does not live on either the top or the bottom floor. Miller lives on a higher floor than does Cooper.
|
||||
Smith does not live on a floor adjacent to Fletcher's. Fletcher does not live on a floor adjacent to Cooper's.
|
||||
Where does everyone live?"
|
||||
|
||||
var problem2 = "Baker, Cooper, Fletcher, Miller, Guinan, and Smith
|
||||
live on different floors of an apartment house that contains
|
||||
only seven floors. Guinan does not live on either the top or the third or the fourth floor.
|
||||
Baker does not live on the top floor. Cooper
|
||||
does not live on the bottom floor. Fletcher does not live on
|
||||
either the top or the bottom floor. Miller lives on a higher
|
||||
floor than does Cooper. Smith does not live on a floor
|
||||
adjacent to Fletcher's. Fletcher does not live on a floor
|
||||
adjacent to Cooper's. Where does everyone live?"
|
||||
|
||||
[demo1, demo2, problem1, problem2].each{|problem| say dinesman(problem).join("\n"); say '' };
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
var names = %w(Baker Cooper Fletcher Miller Smith)
|
||||
|
||||
var predicates = [
|
||||
->(c){ :Baker != c.last },
|
||||
->(c){ :Cooper != c.first },
|
||||
->(c){ (:Fletcher != c.first) && (:Fletcher != c.last) },
|
||||
->(c){ c.index(:Miller) > c.index(:Cooper) },
|
||||
->(c){ (c.index(:Smith) - c.index(:Fletcher)).abs != 1 },
|
||||
->(c){ (c.index(:Cooper) - c.index(:Fletcher)).abs != 1 },
|
||||
]
|
||||
|
||||
names.permutations { |candidate|
|
||||
if (predicates.all {|predicate| predicate(candidate) }) {
|
||||
say candidate.join("\n")
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
# Input: an array representing the apartment house, with null at a
|
||||
# particular position signifying that the identity of the occupant
|
||||
# there has not yet been determined.
|
||||
# Output: an elaboration of the input array but including person, and
|
||||
# satisfying cond, where . in cond refers to the placement of person
|
||||
def resides(person; cond):
|
||||
range(0;5) as $n
|
||||
| if (.[$n] == null or .[$n] == person) and ($n|cond) then .[$n] = person
|
||||
else empty # no elaboration is possible
|
||||
end ;
|
||||
|
||||
# English:
|
||||
def top: 4;
|
||||
def bottom: 0;
|
||||
def higher(j): . > j;
|
||||
def adjacent(j): (. - j) | (. == 1 or . == -1);
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
[]
|
||||
| resides("Baker"; . != top) # Baker does not live on the top floor
|
||||
| resides("Cooper"; . != bottom) # Cooper does not live on the bottom floor
|
||||
| resides("Fletcher"; . != top and . != bottom) # Fletcher does not live on either the top or the bottom floor.
|
||||
| index("Cooper") as $Cooper
|
||||
| resides("Miller"; higher( $Cooper) ) # Miller lives on a higher floor than does Cooper
|
||||
| index("Fletcher") as $Fletcher
|
||||
| resides("Smith"; adjacent($Fletcher) | not) # Smith does not live on a floor adjacent to Fletcher's.
|
||||
| select( $Fletcher | adjacent( $Cooper ) | not ) # Fletcher does not live on a floor adjacent to Cooper's.
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
$ jq -n -f Dinesman.jq
|
||||
[
|
||||
"Smith",
|
||||
"Cooper",
|
||||
"Baker",
|
||||
"Fletcher",
|
||||
"Miller"
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue