Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > sci.math > #501651

Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI !

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups sci.math
Subject Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI !
Date 2020-10-13 01:06 +0100
Organization A noiseless patient Spider
Message-ID <87ft6j56ql.fsf@bsb.me.uk> (permalink)
References (4 earlier) <rm2ik4$o92$1@dont-email.me> <87imbf6qs2.fsf@bsb.me.uk> <rm2m4r$g20$1@dont-email.me> <87d01n6o7o.fsf@bsb.me.uk> <rm2p3q$vso$1@dont-email.me>

Show all headers | View raw


Basil Jet <basil@spamspamspam.com> writes:

> On 13/10/2020 00:04, Ben Bacarisse wrote:
>> Basil Jet <basil@spamspamspam.com> writes:
>>
>>> On 12/10/2020 23:08, Ben Bacarisse wrote:
>> <cut>
>>>> I look forward to seeing your solution.  I'll be upset if it is as
>>>> simple as mine since, it clearly generates shorter solutions.
>>>
>>> There is a 4 line function that moves a disc and prints it, and a 12
>>> line recursive function which does nothing but call itself, call the
>>> other function and perform various "if.. else" statements.
>>
>> Sounds very neat.  I look forward to seeing it.  Mine does not actually
>> move anything.  All it does is print the moves required.
>>
>
> It's in Swift, but there's nothing non-standard in it, apart form the
> explanation marks and underscores, which can be ignored. The towers
> are numbered from 0 to 2, instead of 1 to 3.

<snip code>

Thanks.  I'll take a look later (getting late here now).  Mine is in C.
It is just a plan ToH solver with a loop that gathers the rings
together.  To get the rings on pin 1 (rather than 0) I just added the
call move(max_d, find_ring(1, pins), 1); at the end.  All but the last
two lines of main are just collecting and checking the configuration
since that's given on the command line.

#include <stdio.h>
#include <stdlib.h>

int find_ring(int n, const unsigned pins[])
{
     for (int p = 0; p < 3; p++)
          if (pins[p] & (1u << n))
               return p;
     return -1;
}

void move(int n_rings, int from, int to)
{
     if (from != to && n_rings > 0) {
          int free_pin = 3 - from - to;
          move(n_rings-1, from, free_pin);
          printf("%c%c\n", 'A' + from, 'A' + to);
          move(n_rings-1, free_pin, to);
     }
}

int main(int argc, char **argv)
{
     unsigned pins[3] = {0};

     int pin = 0, max_d = 0;
     for (int a = 1; a < argc; a++)
          if (*argv[a] >= 'A' && *argv[a] < 'A' + 3 && !argv[a][1])
               pin = *argv[a] - 'A';
          else {
               int d = atoi(argv[a]);
               if (d > max_d)
                    max_d = d;
               pins[pin] |= 1u << d;
          }

     for (int p = 1; p < 3; p++)
          for (int r = 0; r < p; r++)
               if (pins[p] & pins[r]) {
                    fprintf(stderr, "Duplicate rings.\n");
                    return EXIT_FAILURE;
               }

     if ((pins[0] | pins[1] | pins[2]) != (1u << max_d+1) - 2) {
          fprintf(stderr, "Rings do not form a contiguous range from 1.\n");
          return EXIT_FAILURE;
     }

     for (int n = 1; n < max_d; n++)
          move(n, find_ring(n, pins), find_ring(n + 1, pins));
}

-- 
Ben.

Back to sci.math | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

$500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Graham Cooper <grahamcooper7@gmail.com> - 2020-10-12 05:26 -0700
  Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Basil Jet <basil@spamspamspam.com> - 2020-10-12 14:50 +0100
    Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Graham Cooper <grahamcooper7@gmail.com> - 2020-10-12 12:07 -0700
      Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-10-12 15:30 -0700
        Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-10-12 15:31 -0700
          Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Graham Cooper <grahamcooper7@gmail.com> - 2020-10-12 15:41 -0700
            Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-10-12 16:22 -0700
              Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Graham Cooper <grahamcooper7@gmail.com> - 2020-10-12 16:27 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-10-12 16:36 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-10-12 16:37 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Graham Cooper <grahamcooper7@gmail.com> - 2020-10-12 16:46 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-10-12 17:49 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-10-12 17:50 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-10-12 17:52 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-10-12 17:53 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-10-12 18:02 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-10-12 18:05 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Sergio <invalid@invalid.com> - 2020-10-12 20:05 -0500
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-10-12 18:20 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Graham Cooper <grahamcooper7@gmail.com> - 2020-10-12 18:22 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-10-12 18:24 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-10-12 18:29 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-10-12 18:49 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Sergio <invalid@invalid.com> - 2020-10-12 22:50 -0500
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-10-12 21:25 -0700
            Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-10-12 16:26 -0700
              Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Graham Cooper <grahamcooper7@gmail.com> - 2020-10-12 16:38 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Peter <peterxpercival@hotmail.com> - 2020-10-13 01:47 +0100
  Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-10-12 21:01 +0100
    Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Graham Cooper <grahamcooper7@gmail.com> - 2020-10-12 13:12 -0700
      Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Basil Jet <basil@spamspamspam.com> - 2020-10-12 21:24 +0100
        Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Peter <peterxpercival@hotmail.com> - 2020-10-12 21:48 +0100
          Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Graham Cooper <grahamcooper7@gmail.com> - 2020-10-12 13:57 -0700
          Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-10-12 22:49 +0100
      Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Sergio <invalid@invalid.com> - 2020-10-12 15:25 -0500
      Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-10-12 22:30 +0100
        Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Basil Jet <basil@spamspamspam.com> - 2020-10-12 22:42 +0100
          Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-10-12 23:08 +0100
            Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Dario Alpern <darioalpern@gmail.com> - 2020-10-12 15:23 -0700
              Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-10-13 00:06 +0100
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Graham Cooper <grahamcooper7@gmail.com> - 2020-10-12 16:25 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-10-13 01:24 +0100
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-10-12 17:54 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Dario Alpern <darioalpern@gmail.com> - 2020-10-12 17:29 -0700
            Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Basil Jet <basil@spamspamspam.com> - 2020-10-12 23:42 +0100
              Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-10-13 00:04 +0100
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Basil Jet <basil@spamspamspam.com> - 2020-10-13 00:33 +0100
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Graham Cooper <grahamcooper7@gmail.com> - 2020-10-12 16:40 -0700
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-10-13 01:06 +0100
                Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-10-13 01:26 +0100
  Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Archimedes Plutonium <plutonium.archimedes@gmail.com> - 2020-10-12 13:58 -0700
  Re: $500 MATHS CHALLENGE ..... just solve this TOWERS OF HANOI ! Dario Alpern <darioalpern@gmail.com> - 2020-10-12 14:33 -0700

csiph-web