Dinesman's multiple-dwelling problem: Difference between revisions
Content deleted Content added
m Fixed small typo |
Small code enhancements, some comments on maintainability. |
||
Line 212: | Line 212: | ||
This program simply iterates through all numbers between 01234 and 43210 (base 5). To see whether this is a permutation worth testing, a binary mask is generated. If all 5 bits are set (31 decimal), this is a possible candidate. Then all ASCII digits of the generated number are converted back to numbers by subtracting the value of ASCII "0". Finally each of the conditions is tested. |
This program simply iterates through all numbers between 01234 and 43210 (base 5). To see whether this is a permutation worth testing, a binary mask is generated. If all 5 bits are set (31 decimal), this is a possible candidate. Then all ASCII digits of the generated number are converted back to numbers by subtracting the value of ASCII "0". Finally each of the conditions is tested. |
||
All conditions are confined to a single word. The algorithm "as is" will work up to 10 floors. After that, we have to take into consideration that characters A-Z are used as digits. That will work up to 36 floors. |
|||
Although this is not ANS Forth, one should have little trouble converting it. |
Although this is not ANS Forth, one should have little trouble converting it. |
||
Line 256: | Line 258: | ||
then false \ nice try, no cigar.. |
then false \ nice try, no cigar.. |
||
; |
; |
||
( a --) |
|||
: .solution #floor 0 do i names i chars over + c@ 1+ emit cr loop drop ; |
|||
\ main routine |
\ main routine |
||
: dinesman ( --) |
: dinesman ( --) |
||
2932 194 do |
|||
2932 194 do i perm? if solution? if leave else drop then else drop then loop |
|||
i perm? if solution? if .solution leave else drop then else drop then |
|||
loop |
|||
; \ show the solution |
; \ show the solution |
||
dinesman |
|||
dinesman</lang> |
dinesman</lang> |