The search for the “perfect” Advent Calendar using Python and Processing #Python

advent calendar

John Graham-Cumming posts about a search for the best randomization of doors on an Advent calendar to maximize distance between consecutively numbered doors.

I grew up with Advent Calendars, and they are very common in the UK. Shops across the country sell calendars with typically 24 doors on them, one for each day from December 1 to December 24. Behind each door is a small gift (usually chocolate or something similarly sweet and edible).

The numbers on the doors of the calendar are usually arranged somewhat haphazardly. Part of the fun each day is finding the next door to open. It’s the search for the chocolate that makes the calendars enjoyable. Here’s an example layout from an Advent Calendar that I bought in Paul in London:

This is an example of a very common 6×4 (and sometimes 4×6) layout for calendars. In this blog I’m going to develop code to find “pleasing” Advent Calendar layouts in the 6×4 shape. But first, here’s a little animation of the Paul calendar in action. What makes it “pleasing” is that the numbers don’t cluster together (mostly).

So, that calendar isn’t bad but it would be nice to generate a calendar where the numbers are spread about such that from day to day you have to hunt for the next number. The number you are searching for won’t be near yesterday’s number and if you see a number close to it then you are likely in the wrong place. That latter restriction adds a lot of pleasure because we are so used to seeing consecutive numbers that our brains instantly start looking nearby.
To do that I broke out the Python (I’m no Python expert so I appreciate Pythonistas who write in with improvements to my code).
Here’s a layout with score 378.277000309

Here is John’s post for further details and this Hacker News thread with more optimal solutions.