I did spend a little time trying to reason it out and arrive at an answer through logic but soon gave up and resorted to my usual method of 'try all the numbers and count how many fit'.
Using my trusty Minstrel Forth (Jupiter Ace clone) I have written a program to cycle through all 10-digit numbers that use 1,2 and 3 and test / count the number that qualify (adjacent numbers differ by 1)
It's taking a while. As I write this it seems to be running but it'll take a little while longer before I have an answer. Once it's finished and I know it's working properly I'll write my program below for Forth fans.
[update]
The answer is 64 and this week we do have some working.
My program did eventually come up with the right answer after a bit of debugging and you can see the pattern referred to. The listing is below, please feel free to type it in, or paste it if you have one of my USB keyboard / serial adaptors.
I love using recursion. Here I've re-used some words that I'd already created for updating the score in a game. If the word detects that the byte has gone above 9 (or in this case above 3) then it resets that byte, decrements the memory address and calls itself. It doesn't have a check on the number of recursions it makes, but the main program detects when it's finished (there are 11 bytes in the score table, and when the leftmost ticks over, then the program has finished.)
Note that Jupiter Ace Forth allows you to simply call a word from within that word, some flavours don't allow recursion or use the word RECURSE to call the current word.
create score_table 0 c, 0 c, 0 c, 0 c, 0 c, 0 c, 0 c, 0 c, 0 c, 0 c, 0 c,
0 variable total
: inc_recursive ( a--a )
dup dup c@ 1 +
swap c! dup c@ 4 =
if
dup 1 swap c! 1 - inc_recursive
then ;
: inc_score ( -- )
score_table 10 + inc_recursive drop ;
: qualifies ( a--f )
dup c@ swap 1 + c@ - abs 1 = ;
: num_qualify ( --n )
0 10 1
do
score_table i + qualifies
if
1 +
then
loop ;
: clear ( -- )
11 0 do
1 score_table i + c!
loop
0 total !
;
: finished ( --f )
score_table c@ 2 =
;
: print_score ( -- )
11 1
do
score_table i + c@ .
loop
cr ;
: run
clear
begin
inc_score num_qualify 9 =
if
total @ 1 + total !
print_score
then
finished
until
cr total @ .
;
Comments
Post a Comment