Dinesman's multiple-dwelling problem: Difference between revisions

m
→‎{{header|REXX}}: simplified the code.
m (→‎{{header|REXX}}: edlided a duplicate variable name, simplified a statement.)
m (→‎{{header|REXX}}: simplified the code.)
Line 3,251:
 
The "rules" that contain   '''=='''   could be simplified to   '''='''   for readability.
 
<lang rexx>/*REXX program solves the Dinesman's multiple─dwelling problem with "natural" wording.*/
names= 'Baker Cooper Fletcher Miller Smith' /*names of multiple─dwelling tenants. */
#tenants= words(names) /*the number of tenants in the building*/
floors= 5; top=floors; bottom=1 top= floors; bottom= 1 /*floor 1 is the ground (bottom) floor.*/
sols= 0 /*the number of solutions found so far.*/
do !@.1=1 for floors /*iterate through all floors for rules.*/
do !@.2=1 for floors /* " " " " " " */
do !@.3=1 for floors /* " " " " " " */
do !@.4=1 for floors /* " " " " " " */
do !@.5=1 for floors /* " " " " " " */
do p=1 for #tenants; _= word(names,p); upper _; call value _, !.pset
do j=1 end for floors-1; a= @.j /*p [↓] people don't live on same floor*/
do k=j+1 to floors /*see if any people live on same floor.*/
 
if a==@.k then doiterate @.5 j=1 for floors-1 /*Is [↓] peopleanyone don'tcohabiting? live onThen samenot floorvalid*/
do k=j+1 to floorsend /*see if any people live on same floor.k*/
if !.j==!.k then iterate !.5 end /*cohab? Then not valid.j*/
call Waldo end /*k ◄══ where the rubber meets the road.*/
end end /*j@.5*/
end /*!@.34*/
 
call Waldo end /* ◄══ where the rubber meets the road@.3*/
end end /*!@.52*/
end end /*!@.41*/
end /*!.3*/
end /*!.2*/
end /*!.1*/
 
say 'found ' sols " solution"s(sols). /*display the number of solutions found*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
set: do p=1 for #tenants; call value word(names, p), @.p; end; return
s: if arg(1)=1 then return ''; return "s" /*a simple pluralizer function.*/
th: arg x; x=abs(x); return word('th st nd rd', 1 +x// 10* (x//100%10\==1)*(x//10<4))
Line 3,291 ⟶ 3,290:
sols= sols + 1 /* [↑] "|" is REXX's "or" comparator.*/
say; do p=1 for #tenants; tenant= word(names, p)
say right(tenant, 35) 'lives on the' !@.p || th(!@.p) "floor."
end /*p*/ /* [↑] "||" is REXX's concatenation. */
return /* [↑] show tenants in order in NAMES.*/</lang>