Dinesman's multiple-dwelling problem: Difference between revisions
Content added Content deleted
(J) |
|||
Line 144: | Line 144: | ||
}</lang> |
}</lang> |
||
<pre>[2, 1, 3, 4, 0]</pre> |
<pre>[2, 1, 3, 4, 0]</pre> |
||
=={{header|J}}== |
|||
This problem asks us to pick from one of several possibilities. We can represent these possibilities as a permutation of the residents' initials, arranged in order from lowest floor to top floor: |
|||
<lang j>possible=: ((i.!5) A. i.5) { 'BCFMS'</lang> |
|||
Additionally, we are given a variety of constraints which eliminate some possibilities: |
|||
<lang j>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</lang> |
|||
The answer is thus: |
|||
<lang j> possible |
|||
SCBFM</lang> |
|||
(bottom floor) Smith, Cooper, Baker, Fletcher, Miller (top floor) |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |