Allocating Secret Santas with a Substitution Cipher using Clojure
Over Christmas I found myself delving back into a bit of Clojure, one such problem I stumbled upon solving was allocating Secret Santas. In this post I will discuss how I went about grouping a given list of names based on certain criteria, and then correctly pairing up each person. From here, I will then highlight how I expanded upon the solution to allow these allocations to be distributed, hidden from prying eyes with a simple ROT13 substitution cipher.
Allocating Secret Santas
The first problem that I had to tackle was loading in a given text file which contained all the given participants and their gender, in CSV form.
The solution I wanted to develop catered for the use-case that certain criteria had to be met to make participants eligible to pair with each other. In this case the invariant was that participants could only buy gifts for the same gender.
To achieve this I first
slurped in the file contents, treating each given line as a new participant.
I then went about breaking each entry into their name and gender.
This in-turn then allowed me to group the given list based on gender (the
second and finally return only the grouped participant names.
Now that we had the grouped names we could begin pairing up each participant.
I solved this with a recursive approach which simply zipped the buyers with another randomly shuffled buyer. In the event that a buyer had been paired with themselves, the action would be repeated.
Once complete I was able to compose these building blocks together.
As participants were allocated within groups I used
mapcat to ensure that the result was a flat listing of all the pairings.
Hiding the Pairings using a Substitution Cipher
Now that I could pair up a given list of grouped participants, I then thought about how this could be easily distributed via a print-out without anyone else knowing each others picks. I decided that a simple substitution cipher such as ROT13 would do the trick. I felt it would provide just enough friction to make it hard to work out based on a quick gaze, for example.
To achieve this I first created a simple ROT13 implementation which catered for lower-case alphabet substitutions. This lead me to think that although the value would be a cipher, the length did not change and based on the provided names this could possibly disclosure a pairing.
To resolve this issue I decided to pad each given name with equal amounts of random alphabet values based on the maximum participants name length. This padding was applied to both the beginning and end of the cipher, enclosing the real name in the middle.
Finally, I was able to decorate the
secret-santa function with the ability to return cipher text.
This ensured that all paired up receivers were hidden and required a little work to uncover.
I envision being able to use the solution this coming December, printing and cutting out each of the pairings to distribute to participants.