Data update
This commit is contained in:
parent
81fd053722
commit
52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions
|
|
@ -1 +1,3 @@
|
|||
possible=: ((i.!5) A. i.5) { 'BCFMS'
|
||||
'B C F M S'=:<"1|: P=:(!A.&i.])5 NB. perm matrix and named columns
|
||||
Cs=: (B~:4),(C~:0),(F~:4),(F~:0),(M>C),(1<|S-F),:(1<|F-C) NB. constraints
|
||||
'BCFMS'/:{.P#~*./Cs NB. join constraints; filter; apply resulting permutation
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
possible=: (#~ 'B' ~: {:"1) possible NB. Baker not on top floor
|
||||
possible=: (#~ 'C' ~: {."1) possible NB. Cooper not on bottom floor
|
||||
possible=: (#~ 'F' ~: {:"1) possible NB. Fletcher not on top floor
|
||||
possible=: (#~ 'F' ~: {."1) possible NB. Fletcher not on bottom floor
|
||||
possible=: (#~ </@i."1&'CM') possible NB. Miller on higher floor than Cooper
|
||||
possible=: (#~ 0 = +/@E."1~&'SF') possible NB. Smith not immediately below Fletcher
|
||||
possible=: (#~ 0 = +/@E."1~&'FS') possible NB. Fletcher not immediately below Smith
|
||||
possible=: (#~ 0 = +/@E."1~&'CF') possible NB. Cooper not immediately below Fletcher
|
||||
possible=: (#~ 0 = +/@E."1~&'FC') possible NB. Fletcher not immediately below Cooper
|
||||
'B C F M S'=:<"1|: P=:(!A.&i.])5 NB. perm matrix and named columns
|
||||
|
||||
NB. parse constraints -> fold -> filter:
|
||||
'BCFMS'/:{.P#~*./ ".;._2 (0 :0)
|
||||
B~:4 NB. Baker not on 5th floor
|
||||
C~:0 NB. Cooper not on 1st floor
|
||||
F~:4 NB. Fletcher not on 5th floor...
|
||||
F~:0 NB. ... nor on 1st floor
|
||||
M>C NB. Miller on higher floor than Cooper
|
||||
1<|S-F NB. Smith and Fletcher not on adjacent floors
|
||||
1<|F-C NB. Fletcher and Cooper not on adjacent floors
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,2 +1,4 @@
|
|||
possible
|
||||
SCBFM
|
||||
P=: (!A.&i.])5 NB. permutation matrix
|
||||
'`B C F M S'=: ,{{y&{"1`''}}&>i.5 NB. e.g., B is 0&{"1
|
||||
Cs=: (B~:4:)`(C~:0:)`(F~:4:)`(F~:0:)`(M>C)`(1<S|@:-F)`(1<F|@:-C) NB. gerund constraints
|
||||
'BCFMS' /: {. P [F..{{(x`:6#])y}} Cs NB. fold, filtering as we go
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
P=: ({~(!A.&i.])@#)'BCFMS' NB. all permutations (orderings)
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
P=: (#~ 'B' ~: {:"1) P NB. Baker not on top floor
|
||||
P=: (#~ 'C' ~: {."1) P NB. Cooper not on bottom floor
|
||||
P=: (#~ 'F' ~: {:"1) P NB. Fletcher not on top floor
|
||||
P=: (#~ 'F' ~: {."1) P NB. Fletcher not on bottom floor
|
||||
P=: (#~ </@i."1&'CM') P NB. Miller on higher floor than Cooper
|
||||
P=: (#~ 0 = +/@E."1~&'SF') P NB. Smith not immediately below Fletcher
|
||||
P=: (#~ 0 = +/@E."1~&'FS') P NB. Fletcher not immediately below Smith
|
||||
P=: (#~ 0 = +/@E."1~&'CF') P NB. Cooper not immediately below Fletcher
|
||||
P=: (#~ 0 = +/@E."1~&'FC') P NB. Fletcher not immediately below Cooper
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
P
|
||||
SCBFM
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
(({~(!A.&i.])@#)'BCFMS') [F..(128!:2"1 # ]) ];._2 (0 :0)
|
||||
'B' ~: {: NB. Baker not on top floor
|
||||
'C' ~: {. NB. Cooper not on bottom floor
|
||||
'F' ~: {: NB. Fletcher not on top floor
|
||||
'F' ~: {. NB. Fletcher not on bottom floor
|
||||
</@i.&'CM' NB. Miller on higher floor than Cooper
|
||||
0 = +/@E.~&'SF' NB. Smith not immediately below Fletcher
|
||||
0 = +/@E.~&'FS' NB. Fletcher not immediately below Smith
|
||||
0 = +/@E.~&'CF' NB. Cooper not immediately below Fletcher
|
||||
0 = +/@E.~&'FC' NB. Fletcher not immediately below Cooper
|
||||
)
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
;; Iterate over all permutations of a list, and
|
||||
;; call a function on each.
|
||||
(define (permute permute.seq permute.func (permute.built '()))
|
||||
(if (null? permute.seq)
|
||||
(permute.func permute.built)
|
||||
(let (seq (copy permute.seq))
|
||||
(dotimes (i (length seq))
|
||||
(unless (zero? i) (rotate seq -1))
|
||||
(permute
|
||||
(rest seq)
|
||||
permute.func
|
||||
(cons (first seq) permute.built))))))
|
||||
|
||||
(define (adjacent a b lst)
|
||||
(= 1 (abs (- (find a lst)
|
||||
(find b lst)))))
|
||||
|
||||
(define (check lst)
|
||||
(if
|
||||
(and
|
||||
(< (find 'baker lst) 4)
|
||||
(> (find 'cooper lst) 0)
|
||||
(not (member (find 'fletcher lst) '(0 4)))
|
||||
(> (find 'miller lst) (find 'cooper lst))
|
||||
(not (adjacent 'smith 'fletcher lst))
|
||||
(not (adjacent 'cooper 'fletcher lst)))
|
||||
(println lst)))
|
||||
|
||||
(permute '(baker cooper fletcher miller smith) check)
|
||||
|
||||
(smith cooper baker fletcher miller)
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
const
|
||||
Baker = 0; Cooper = 1; Fletcher = 2; Miller = 3; Smith = 4;
|
||||
names: array of string = ('Baker', 'Cooper', 'Fletcher', 'Miller', 'Smith');
|
||||
|
||||
begin
|
||||
var floors := arr(1..5);
|
||||
repeat
|
||||
if (floors[Baker] <> 5) and
|
||||
(floors[Cooper] <> 1) and
|
||||
(floors[Fletcher] not in [1, 5]) and
|
||||
(floors[Miller] > floors[Cooper]) and
|
||||
(abs(floors[Smith] - floors[Fletcher]) <> 1) and
|
||||
(abs(floors[Fletcher] - floors[Cooper]) <> 1) then
|
||||
begin
|
||||
foreach var floor in floors index person do
|
||||
println(names[person], 'lives on floor', floor);
|
||||
exit
|
||||
end;
|
||||
until not NextPermutation(floors);
|
||||
println('No solution found.');
|
||||
end.
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
PRINT "Los apartamentos están numerados del 0 (bajo) al 4 (ático)."
|
||||
PRINT "Baker, Cooper, Fletcher, Miller y Smith viven en apartamentos diferentes."
|
||||
PRINT "- Baker no vive en el último apartamento (ático)."
|
||||
PRINT "- Cooper no vive en el piso inferior (bajo)."
|
||||
PRINT "- Fletcher no vive ni en el ático ni en el bajo."
|
||||
PRINT "- Miller vive en un apartamento más alto que Cooper."
|
||||
PRINT "- Smith no vive en un apartamento adyacente al de Fletcher."
|
||||
PRINT "- Fletcher no vive en un apartamento adyacente al de Cooper."
|
||||
PRINT
|
||||
FOR baker = 0 TO 3
|
||||
FOR cooper = 1 TO 4
|
||||
FOR fletcher = 1 TO 3
|
||||
FOR miller = 0 TO 4
|
||||
FOR smith = 0 TO 4
|
||||
IF baker <> cooper AND baker <> fletcher AND baker <> miller AND baker <> smith AND cooper <> fletcher THEN
|
||||
IF cooper <> miller AND cooper <> smith AND fletcher <> miller AND fletcher <> smith AND miller <> smith THEN
|
||||
IF miller > cooper AND ABS(smith - fletcher) <> 1 AND ABS(fletcher - cooper) <> 1 THEN
|
||||
PRINT "Baker vive en el piso "; baker
|
||||
PRINT "Cooper vive en el piso "; cooper
|
||||
PRINT "Fletcher vive en el piso "; fletcher
|
||||
PRINT "Miller vive en el piso "; miller
|
||||
PRINT "Smith vive en el piso "; smith
|
||||
END IF
|
||||
END IF
|
||||
END IF
|
||||
NEXT smith
|
||||
NEXT miller
|
||||
NEXT fletcher
|
||||
NEXT cooper
|
||||
NEXT baker
|
||||
END
|
||||
|
|
@ -1,24 +1,29 @@
|
|||
// We add a zero to be able to express e.g. "not top floor"
|
||||
def floors: [0..5 -> (floor:$)];
|
||||
def names: [['Ground', 'Baker', 'Cooper', 'Fletcher', 'Miller', 'Smith']... -> (name:$)];
|
||||
permutations templates
|
||||
when <|=1> do [1] !
|
||||
otherwise
|
||||
n is $;
|
||||
expand templates
|
||||
p is $;
|
||||
1..$n -> templates
|
||||
k is $;
|
||||
[$p(..$k - 1)..., $n, $p($k..)...] !
|
||||
end !
|
||||
end expand
|
||||
$n - 1 -> # -> expand !
|
||||
end permutations
|
||||
|
||||
def dwellings: [$floors, $names] -> \(
|
||||
def solver: $ -> EinsteinSolver;
|
||||
names is ['Baker', 'Cooper', 'Fletcher', 'Miller', 'Smith'];
|
||||
|
||||
{name: 'Ground', floor: 0} -> !solver::isFact
|
||||
({name: 'Ground'} solver::nextTo&{byField: :(floor:), bMinusA: [1..4]} {name: 'Baker'}) -> !VOID
|
||||
({name: 'Ground'} solver::nextTo&{byField: :(floor:), bMinusA: [2..5]} {name: 'Cooper'}) -> !VOID
|
||||
({name: 'Ground'} solver::nextTo&{byField: :(floor:), bMinusA: [2..4]} {name: 'Fletcher'}) -> !VOID
|
||||
({name: 'Cooper'} solver::nextTo&{byField: :(floor:), bMinusA: [1..4]} {name: 'Miller'}) -> !VOID
|
||||
({name: 'Fletcher'} solver::nextTo&{byField: :(floor:), bMinusA: [-4..-2, 2..4]} {name: 'Smith'}) -> !VOID
|
||||
({name: 'Fletcher'} solver::nextTo&{byField: :(floor:), bMinusA: [-4..-2, 2..4]} {name: 'Cooper'}) -> !VOID
|
||||
|
||||
[$solver::solutions&{required: 6} ... -> ($ notMatching {| {name: 'Ground'} |})]!
|
||||
\);
|
||||
|
||||
$dwellings -> \[i]('Solution $i;:
|
||||
$... -> '$;
|
||||
';
|
||||
'! \)... -> !OUT::write
|
||||
'No more solutions
|
||||
' -> !OUT::write
|
||||
5 -> permutations -> $names($)
|
||||
-> if <|?($(.. as i; -> if <|='Baker'> -> $i)... matches <~|=5>)>
|
||||
-> if <|?($(.. as i; -> if <|='Cooper'> -> $i)... matches <~|=1>)>
|
||||
-> if <|?($(.. as i; -> if <|='Fletcher'> -> $i)... matches <~|=1|=5>)>
|
||||
-> templates
|
||||
miller is $(.. as i; -> if <|='Miller'> -> $i)...;
|
||||
$ -> if <|?($(.. as i; -> if <|='Cooper'> -> $i)... matches <|..$miller>)> !
|
||||
end
|
||||
-> if <|?(($(.. as i; -> if <|='Smith'> -> $i)...) - ($(.. as i; -> if <|='Fletcher'> -> $i)...) matches <~|=1|=-1>)>
|
||||
-> if <|?(($(.. as i; -> if <|='Cooper'> -> $i)...) - ($(.. as i; -> if <|='Fletcher'> -> $i)...) matches <~|=1|=-1>)>
|
||||
-> $(.. as i; -> '$i;:$;$#10;')
|
||||
-> $(..:-1)
|
||||
-> '$...;$#10;' !
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
// We add a zero to be able to express e.g. "not top floor"
|
||||
def floors: [0..5 -> (floor:$)];
|
||||
def names: [['Ground', 'Baker', 'Cooper', 'Fletcher', 'Miller', 'Smith']... -> (name:$)];
|
||||
|
||||
def dwellings: [$floors, $names] -> \(
|
||||
def solver: $ -> EinsteinSolver;
|
||||
|
||||
{name: 'Ground', floor: 0} -> !solver::isFact
|
||||
({name: 'Ground'} solver::nextTo&{byField: :(floor:), bMinusA: [1..4]} {name: 'Baker'}) -> !VOID
|
||||
({name: 'Ground'} solver::nextTo&{byField: :(floor:), bMinusA: [2..5]} {name: 'Cooper'}) -> !VOID
|
||||
({name: 'Ground'} solver::nextTo&{byField: :(floor:), bMinusA: [2..4]} {name: 'Fletcher'}) -> !VOID
|
||||
({name: 'Cooper'} solver::nextTo&{byField: :(floor:), bMinusA: [1..4]} {name: 'Miller'}) -> !VOID
|
||||
({name: 'Fletcher'} solver::nextTo&{byField: :(floor:), bMinusA: [-4..-2, 2..4]} {name: 'Smith'}) -> !VOID
|
||||
({name: 'Fletcher'} solver::nextTo&{byField: :(floor:), bMinusA: [-4..-2, 2..4]} {name: 'Cooper'}) -> !VOID
|
||||
|
||||
[$solver::solutions&{required: 6} ... -> ($ notMatching {| {name: 'Ground'} |})]!
|
||||
\);
|
||||
|
||||
$dwellings -> \[i]('Solution $i;:
|
||||
$... -> '$;
|
||||
';
|
||||
'! \)... -> !OUT::write
|
||||
'No more solutions
|
||||
' -> !OUT::write
|
||||
Loading…
Add table
Add a link
Reference in a new issue