" keylog = {tuple(int(c) for c in line.strip()) for line in f}"
]
},
{
"cell_type": "markdown",
"id": "043f31a9",
"metadata": {},
"source": [
"If we assume that no characters are repeated in the passcode, then each entry in the keylog gives a [partial ordering](https://en.wikipedia.org/wiki/Partially_ordered_set) of the characters in the passcode. In math jargon, we are then looking for a [linear extension](https://en.wikipedia.org/wiki/Linear_extension) of the partial ordering. This can be found with a [topological sorting](https://en.wikipedia.org/wiki/Topological_sorting) algorithm.\n",
"\n",
"(If characters *are* repeated, we would instead have a [preordering](https://en.wikipedia.org/wiki/Preorder), and a topological sort wouldn't work. Fortunately for us, our assumption turns out to be correct.)\n",
"\n",
"Conveniently, and somewhat interestingly, Python has [graphlib](https://docs.python.org/3/library/graphlib.html) in its standard library, which (as of the time of writing) *only* implements topological sorting - but that's all we need for this problem, so I guess don't look a gift horse in the mouth."
"This work is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International license](https://creativecommons.org/licenses/by-sa/4.0/) and the [BSD Zero Clause license](https://spdx.org/licenses/0BSD.html)."