I was trying to solve Day 16 of the Advent of Code, when I realised that sorting values can speed up your code.

But that was the wrong choice of data structure for this problem.

Normally, I would use the set() data structure, but I was caching the inputs and needed an immutable data structure.

I knew frozenset() was a thing, but when .add() didn't work, I decided to work with tuples instead.

But the values were supposed to be unique, and the sort order didn't matter.

In fact, the sort order got in the way!

While frozensets are a bit cumbersome to "add" values to, which I simply did with frozenset(tuple(old_frozen_set) + (new_val,)), we get an interesting property.

The frozenset of (0,1) is equal to the frozenset of (1,0).

So it's immutable, a bit cumbersome to work with, and not available in all Python versions...

But it is extremely useful to check for memebership and equality for unique items.


Sets are implemented as hash tables, so checking for membership is a O(1) operation.

How neat!