Dinesman's multiple-dwelling problem: Difference between revisions

Added various BASIC dialects (Chipmunk Basic, QBasic and True BASIC)
(Added various BASIC dialects (Chipmunk Basic, QBasic and True BASIC))
 
(7 intermediate revisions by 6 users not shown)
Line 392:
</pre>
 
==={{header|Chipmunk Basic}}===
{{trans|FreeBASIC}}
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="vbnet">100 cls
110 print "Los apartamentos están numerados del 0 (bajo) al 4 (ático)."
120 print "Baker, Cooper, Fletcher, Miller y Smith viven en apartamentos diferentes."
130 print "- Baker no vive en el último apartamento (ático)."
140 print "- Cooper no vive en el piso inferior (bajo)."
150 print "- Fletcher no vive ni en el ático ni en el bajo."
160 print "- Miller vive en un apartamento más alto que Cooper."
170 print "- Smith no vive en un apartamento adyacente al de Fletcher."
180 print "- Fletcher no vive en un apartamento adyacente al de Cooper."
190 print
200 for baker = 0 to 3
210 for cooper = 1 to 4
220 for fletcher = 1 to 3
230 for miller = 0 to 4
240 for smith = 0 to 4
250 if baker <> cooper and baker <> fletcher and baker <> miller and baker <> smith and cooper <> fletcher then
260 if cooper <> miller and cooper <> smith and fletcher <> miller and fletcher <> smith and miller <> smith then
270 if miller > cooper and abs(smith-fletcher) <> 1 and abs(fletcher-cooper) <> 1 then
280 print "Baker vive en el piso ";baker
290 print "Cooper vive en el piso ";cooper
300 print "Fletcher vive en el piso ";fletcher
310 print "Miller vive en el piso ";miller
320 print "Smith vive en el piso ";smith
330 endif
340 endif
350 endif
360 next smith
370 next miller
380 next fletcher
390 next cooper
400 next baker
410 end</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Commodore BASIC}}===
The statements have been rearranged below so as to eliminate the maximum number of iterations; they could all be placed in the innermost loop and achieve the same result, just more slowly.
 
<syntaxhighlight lang="basic">100 T=5:REM TOP FLOOR
110 FOR B=1 TO T
120 : REM BAKER DOES NOT LIVE ON THE TOP FLOOR
130 : IF B=T THEN 420
140 : FOR C=1 TO T
150 : IF C=B THEN 410: REM ONE PERSON PER FLOOR
160 : REM COOPER DOES NOT LIVE ON THE BOTTOM FLOOR
170 : IF C=1 THEN 410
180 : FOR F=1 TO T
190 : IF F=B OR F=C THEN 400: REM ONE PERSON PER FLOOR
200 : REM FLETCHER DOES NOT LIVE ON TOP OR BOTTOM
210 : IF F=1 OR F=T THEN 400
220 : REM FLETCHER DOES NOT LIVE ADJACENT TO COOPER
230 : IF ABS(F-C)=1 THEN 400
240 : FOR M=1 TO T
250 : IF M=B OR M=C OR M=F THEN 390: REM ONE PERSON PER FLOOR
260 : REM MILLER LIVES ABOVE COOPER
270 : IF M < C THEN 390
280 : FOR S=1 TO T
290 : IF S=B OR S=C OR S=F OR S=M THEN 380: REM ONE PERSON PER FLOOR
300 : REM SMITH DOES NOT LIVE ADJACENT TO FLETCHER
310 : IF ABS(F-S)=1 THEN 380
320 : PRINT "BAKER IS ON"B
330 : PRINT "COOPER IS ON"C
340 : PRINT "FLETCHER IS ON"F
350 : PRINT "MILLER IS ON"M
360 : PRINT "SMITH IS ON"S
370 : END
380 : NEXT S
390 : NEXT M
400 : NEXT F
410 : NEXT C
420 NEXT B
430 PRINT "NO SOLUTION"</syntaxhighlight>
 
{{Out}}
<pre>BAKER IS ON 3
COOPER IS ON 2
FLETCHER IS ON 4
MILLER IS ON 5
SMITH IS ON 1</pre>
 
==={{header|FreeBASIC}}===
Line 684 ⟶ 766:
4 miller
0 smith</pre>
 
==={{header|QBasic}}===
{{trans|FreeBASIC}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|True BASIC}}
<syntaxhighlight lang="qbasic">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</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Run BASIC}}===
Line 709 ⟶ 830:
print "Can't assign rooms" ' print this if it can not find a solution</syntaxhighlight>
<pre>baler: 3 copper: 2 fletcher: 4 miller: 5 smith: 1</pre>
 
==={{header|True BASIC}}===
{{trans|FreeBASIC}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">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</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|uBasic/4tH}}===
Line 1,304 ⟶ 1,462:
 
=={{header|D}}==
 
 
{{incorrect|D| <br><br> The output is incorrect: <br><br>
it has Fletcher on the bottom floor, <br>
Baker on the top, <br>
and Cooper and Fletcher adjacent. <br><br>}}
 
 
This code uses the second lazy permutations function of '''[[Permutations#Lazy_version]]'''.
 
As for flexibility: the solve code works with an arbitrary number of people and predicates.
<syntaxhighlight lang="d">import std.stdio, std.math, std.algorithm, std.traits, permutations2;
import std.stdio, std.math, std.algorithm, std.traits, std.array, permutations2:permutations;
 
void main() {
 
enum Names { Baker, Cooper, Fletcher, Miller, Smith }
 
immutable(bool function(in Names[]) pure nothrow)[] predicates = [
s => s[.countUntil(Names.Baker]) != 4 && s.lengthcountUntil(Names.Cooper) - 1!= 0,
s => s.countUntil(Names.Fletcher) != 4 && s => s[.countUntil(Names.Cooper]Fletcher) != 0,
s => s[.countUntil(Names.Fletcher]Miller) != 0 &&> s[.countUntil(Names.Fletcher] != s.length-1Cooper),
s => abs(s[.countUntil(Names.Miller]Smith) >- s[.countUntil(Names.Cooper]Fletcher)) != 1,
s => abs(s[.countUntil(Names.Smith]Cooper) - s[.countUntil(Names.Fletcher])) != 1,
];
s => abs(s[Names.Cooper] - s[Names.Fletcher]) != 1];
 
permutations([EnumMembers!Names]).filter!(solution => predicates.all!(pred => pred(solution)))
.filter!(solution => predicates.all!(pred => pred(solution)))
.writeln;
}</syntaxhighlight>
{{out}}
<pre>[[FletcherSmith, Cooper, MillerBaker, SmithFletcher, BakerMiller]]</pre>
 
===Simpler Version===
Line 1,352 ⟶ 1,505:
 
The output is the same.
 
=={{header|EasyLang}}==
{{trans|11l}}
<syntaxhighlight>
proc nextperm . a[] .
n = len a[]
k = n - 1
while k >= 1 and a[k + 1] <= a[k]
k -= 1
.
if k = 0
a[] = [ ]
return
.
l = n
while a[l] <= a[k]
l -= 1
.
swap a[l] a[k]
k += 1
while k < n
swap a[k] a[n]
k += 1
n -= 1
.
.
for i = 1 to 5
floors[] &= i
.
BAKER = 1
COOPER = 2
FLETCHER = 3
MILLER = 4
SMITH = 5
names$[] = [ "Baker" "Cooper" "Fletcher" "Miller" "Smith" ]
#
repeat
if floors[BAKER] <> 5 and floors[COOPER] <> 1 and floors[FLETCHER] <> 1 and floors[FLETCHER] <> 5
if floors[MILLER] > floors[COOPER] and abs (floors[SMITH] - floors[FLETCHER]) <> 1 and abs (floors[FLETCHER] - floors[COOPER]) <> 1
for i to 5
print names$[i] & " lives on floor " & floors[i]
.
break 1
.
.
nextperm floors[]
until len floors[] = 0
.
</syntaxhighlight>
 
{{out}}
<pre>
Baker lives on floor 3
Cooper lives on floor 2
Fletcher lives on floor 4
Miller lives on floor 5
Smith lives on floor 1
</pre>
 
=={{header|EchoLisp}}==
Line 4,099 ⟶ 4,310:
<pre>
"Smith Cooper Baker Fletcher Miller"
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang = "rust">use itertools::Itertools;
 
 
fn main() {
for p in (1..6).permutations(5) {
let baker: i32 = p[0];
let cooper: i32 = p[1];
let fletcher: i32 = p[2];
let miller: i32 = p[3];
let smith: i32 = p[4];
if baker != 5 && cooper != 1 && fletcher != 1 && fletcher != 5 && cooper < miller &&
(smith - fletcher).abs() > 1 && (cooper - fletcher).abs() > 1 {
print!("Baker on {baker}, Cooper on {cooper}, ");
println!("Fletcher on {fletcher}, Miller on {miller}, Smith on {smith}.");
break;
}
}
}
</syntaxhighlight>{{out}}
<pre>
Baker on 3, Cooper on 2, Fletcher on 4, Miller on 5, Smith on 1.
</pre>
 
Line 4,243 ⟶ 4,478:
 
# Build an array of lambda's
var predicates = lines.ftfirst(-1, lines).endlast(-1).map{ |line|
var keywords = line.scan(re_keywords)
var (name1, name2) = line.scan(re_names)...
Line 4,621 ⟶ 4,856:
{{trans|Kotlin}}
{{libheader|Wren-seq}}
<syntaxhighlight lang="ecmascriptwren">import "./seq" for Lst
 
var permute // recursive
2,130

edits