Tuesday, January 10, 2012

Enigmatic Python

While attempting to find a programmatic solution to Enigma 1602 (which really is much easier to do with pencil and paper) for the Enigmatic Code Blog, I briefly toyed with the idea of writing my own code to solve sets of linear simultaneous equations.

Then I discovered the rather marvellous SymPy library, which can do that and much more, and made for a very neat solution.

I've really only scratched the surface of this module, but I expect to use it more in the future.

Along with other Python modules that aid in the writing Enigma solutions: unlimited precision integers, set, itertools, collections, fractions, probably lots of other standard modules I have yet to discover, and, of course, my own set of useful routines.

And finally, to squeeze that bit extra out your Python programs you can always try using PyPy, which is a Python interpreter, written in Python. It includes a JIT compiler and often ends up running faster than the standard CPython interpreter.

I tried it on my code for Enigma 1653 - one of the trickier ones - and I got the following runtimes:

CPython 2.7.1: 2m01s
CPython 3.2: 1m52s
PyPy: 0m10s

Thursday, December 1, 2011

Enigmatic Code

For the past few years I've been treating the weekly Enigma puzzles in New Scientist magazine as a programming challenge. I started writing programs to solve the puzzles in Perl (and occasionally C, when speed was all important), but more recently I've been coding up solutions in Python.

Now, I've decided to set up a blog to share programmatic solutions to Enigma puzzles. I opted for a WordPress blog, as I couldn't get Blogger comments to accept pre-formatted code. Whereas WordPress comments let you use HTML <pre> tags.

I aim to add new puzzles (and solutions) as they are published (usually on a Thursday in the UK), and also retroactively add old puzzles that I have coded solutions for, or found from the New Scientist archives on Google Books, as time allows.

If you'd like to join in, please visit the Enigmatic Code blog.

Monday, September 26, 2011

Dice Emulator

The Merry Game of Floundering
The other day Caroline came home with a charity shop find - The Merry Game of Floundering* - which I'm sure we used to play as kids (although my Mum claims never to have heard of it).

All the pieces were there with the exception of one of the dice (it needs to two). So rather than search for another die I did what any self-respecting programmer would do, and knocked up a dice emulator in Python.

Thanks to the pygame library and Unicode characters U+2680 - U+2685 I was able to get a program together quickly which we then triggered using the Apple Remote (using Remote Buddy's Virtual Mouse behaviour). Simple as that!

dice.py
The program is presented below. Be aware, though, it makes some assumptions about running on my MacBook. If you're running it on non-1280x800 screen size you'll probably need to change the font sizes, and if you're running on a non-Apple system you'll probably need to change the font for the die characters (or go back to using ASCII numbers, as it was originally), and the location of the sound that is played when the dice is rolled. Also there is some code there to highlight special scores (doubles, and 6 and 1), which are specific to the Floundering game - take them out, or define your own. Enjoy!



[*] While searching for more information on the game I found someone selling a set on eBay as "The Messy Game of Floundering".

Monday, August 8, 2011

Gotta Catch 'Em All

"Fade to Black"
From 6th July 2011 to 7th September 2011 there are 60 decorated gorilla statues roaming the (mostly) Bristol area. (See the Wow! Gorillas page for for details).

We're now half way through their visit and I've managed to collect 53 out of 60. You can see my efforts in my Facebook photo gallery. (Facebook registration not required).

So I'm missing 7:
  • Gorilla 5 ("Sky Gorilla"), Anchor Square, Bristol Harbour.
  • Gorilla 9 ("Willard"), A Marriott Hotel Lobby, Bristol.
  • Gorilla 25 ("Elvis"), Bristol Bus Station.
  • Gorilla 26 ("Hubert"), Holiday Inn, Bristol.
  • Gorilla 29 ("Ape Scape"), Bristol Airport.
  • Gorilla 40 ("Winston"), Avon Gorge Hotel, Bristol.
  • Gorilla 60 ("Guerilla Tourist"), Birmingham(!) Coach Station.
I missed #5 on my visit to Bristol Harbour - I think it must have been obscured by a fairground ride that was there.

I foolishly thought that the Marriott Hotel City Centre would be the one by The Centre, but it turns out that that's the Marriott Royal Hotel, and the City Centre one is at Broadmead (I suppose that's "City Centre" when compared to Bristol Airport or somewhere). So I ended up at the wrong hotel. (Although #9 is in the Broadmead one from today).

I don't usually happen to pass through Bristol Airport on my day-to-day travels, but I might be able to get out there sometime in the next month to collect #29.

But Birmingham seems a bit of a way to go. Although I know the closer I get to completing the set the more I will feel compelled to make a trip up to Brum.

But there maybe a solution. Apparently Gorilla #61 ("Doris") is currently floating around Bristol Harbour on the prow of The Matthew, and was organised too late to make it on to the official maps. So there may be hope of sneakily collecting 60 Gorillas after all.

Update 2011-09-04: I'm now on 59/61 - only #29 and #60 to get!

Update 2011-09-06: I went to Bristol Airport and collected #29 today. 60/61.

Update 2011-09-25: I finally collected "Guerilla Tourist" today, now that he's outside Bristol Zoo and no longer in Birmingham. 61/61.

Monday, May 23, 2011

Apocalypse - Not!

So, the Rapture didn't happen on Saturday 21st May 2011. Still, it's not the end of world, eh?

But it was the end of my venerable Compaq Evo N610c that is so good IMDb didn't want it back when I left. The hard disk failed a mere 7½ years after being replaced. Oh well, it's been doing a good job for the last (almost) two years as a firewall machine with Ubuntu installed and a PCMCIA network card.

I recently retired it from firewall duty (as it's on-board network connection seemed to be playing up, although the problem disappeared once I'd retired it), so it was left acting as a local DNS server and a WebDAV server (for sharing iCal calendars). Both of which I would rather the Time Capsule would take care of - but it doesn't.

I've managed to resurrect it for now using the 160GB drive that I removed from my MacBook when I expanded that, attached to the Evo using a cheap USB enclosure, put another Ubuntu install on it and installed dnsmasq and got lighttpd running as a WebDAV server, and everything seems dandy again.

But I can't help thinking I'd be better off with a proper low power server to do this kind of thing.

Wednesday, March 16, 2011

Javascript Speed Tests

Every so often I think about changing the browser that I routinely use. For the last five years or so I've been using Camino, first on my PowerBook, and now on my MacBook.

Today I decided to try the sunspider-0.9.1 Javascript benchmark on a variety of browsers that I have to hand, and here are the results:

  Camino   2.0.6        3085.3ms
  Chrome   10.0.648.134  319.0ms
  Firefox  3.6.15       1014.6ms
  Firefox  4.0           313.8ms
  Opera    11.01         347.9ms
  Safari   5.0.4         333.7ms

So it seems I am currently using the browser with the slowest Javascript performance (by a factor of almost 10x), and although I've become very used to Camino, maybe it's time to consider a switch (again).

Update 2011-03-22: Added in Firefox 4.0 (which is the current leader, and means that all the alternatives are about 10x faster than Camino in this test).

Thursday, February 10, 2011

Top Time Machine Tip

Here's a Top Tip for those using Apple's Time Machine...

If you are backing up multiple computers to a single Time Capsule, and one of the machines decides there isn't enough space to complete a backup, try compacting the other backups - especially if they are quite large.

Open a Terminal and the following command:

# hdiutil compact /path/to/backup/sparse/bundle

I did this on the backup of my machine, when another laptop was deleting old backups trying to make space, and it freed up over 90GB of space. Plenty enough to allow the other machine to complete it's backup.


I turned off automatic backups while it was running - remember to re-enable them afterwards.

This is something that Time Machine will run itself if it's runs out of space, but it will only compact the backup for the machine that it is running on, so if there is a potentially large amount of free space in a backup for a different machine that won't get reclaimed. Instead the first machine will start sacrificing it's older backups to make space.

Note: I'm running Time Machine on OS X 10.6.6