Skip to content

pikesley/towers-of-hanoi

Repository files navigation

Build Status Dependency Status Coverage Status Code Climate License

Towers of Hanoi

Counting in binary to solve the Towers of Hanoi

Surely there are easier ways to do this?

Yes, there are. This is very much a Solved Problem. However, I was inspired to implement this solution after watching 3 Blue 1 Brown's fascinating video, in which Grant relates the Towers Of Hanoi to the Rhythm Of Counting In Binary:

Screenshot

In order to over-engineer this, I've wrapped a very thin Flask app around the pHAT, so that the interesting stuff can be built in Ruby

Running it

make

to run the tests, and

make solve discs=n

to actually solve

Note that the output is kind of sideways, and that's because console output is not the real purpose of this - if you're running this on a Raspberry Pi with a Micro Dot pHAT attached, then try

sudo pip install -r requirements.txt
make phat

to watch this all play out on the pHAT:

A post shared by Sam (@pikesley) on Nov 19, 2017 at 8:04am PST

<script async defer src="//platform.instagram.com/en_US/embeds.js"></script>

Constrained version

There is a constrained variant of the problem, with the restriction that a disc may only move to an adjacent stack. I've also implemented the solution for this (which maps to the rhythms of ternary) - you can run this with

make solve-constrained discs=n

(this is now the default mode for make phat)

Fake pHAT mode

The Flask webserver attempts to detect if it's running on a Pi, and if it thinks it's not then it will log an ASCII rendition of the pHAT matrix on the console:

.....   .....   .....   .....   ooo..   .o...
.....   .....   .....   .....   o.o..   .o...
.....   .....   .....   .....   ooo..   .o...
.....   .....   .....   .....   .....   .....
.ooo.   .....   .....   ..ooo   ..ooo   ..ooo
oooo.   .....   .....   ..o.o   ..o.o   ..o.o
ooooo   .oo..   ..o..   ..ooo   ..ooo   ..ooo

this may or may not be useful (I'm not sure any of this is useful tbh)