ππ One-line Python solutions for Advent of Code 2022 and 2023.
As a personal challenge, I'm trying to solve every Advent of Code problem in a single line of Python code. No, my solutions are not optimal. Nor readable. Nor useful in any other way. But it's fun, so here we go!
I originally attempted this in 2022 and made it through about a dozen days. I'm now working on 2023 in real time! You can follow along on this repository or through my Reddit posts.
Note that per copyright and Eric Wastl's request, input files are not included in this repository, so you must replace the file paths if you would like to run this code.
I'm using Python version 3.11.3
. The solutions should not require any additional packages (I'm only importing from the Python Standard Library), but in the tools folder, I have a script for importing the input files from the Advent of Code server that uses requests
and a visualization tool that uses matplotlib
and svgwrite
.
Status | Description |
---|---|
β | Problem not attempted yet |
β | Working on original solution |
β | Original (OG) solution finished, working on one-line solution |
β | Completed both OG and one-line solutions |
βοΈ | Completed both solutions with help from others on the multi-line part |
Day | Part 1 | Part 2 | Commentary |
---|---|---|---|
01 | β | β | This year, I'm removing my "no imports" restriction π |
02 | β | β | Getting in a lot of RegEx practice this year! |
03 | β | β | Oh boy, the amount of RegEx I'm using is slowing down my computer... and the Walrus is back! |
04 | β | β | Pulling out some dunder (magic) methods for this one! |
05 | β | βοΈ |
lambda s are making their first appearance... And credit to Dylan Gray for his immense help with my Part 2 OG solution! |
06 | β | β |
reduce has joined the fray! |
07 | β | β | First solution I didn't use RegEx... there goes that challenge for myself π |
08 | β | β |
itertools.takewhile is a lifesaver! |
09 | β | β | Gonna be a busy few days so I might fall behind for a bit... |
10 | β | β | Not sure why this one took me until Day 17 to solve, but we finally did it! Proud of my Part 2 π |
11 | β | β | So glad I didn't need to modify my approach for Part 2! |
12 | β | βοΈ | Credit to SΓΈren Fuglede JΓΈrgensen for their solution for Part 2! |
13 | β | β | I did go back and rewrite Part 1 after solving Part 2. This one took me a while for some reason! |
14 | β | β | The hardest part is always the while loops! |
15 | β | β | My fastest solve yet! |
16 | β | β | Let's just ignore the setrecursionlimit(30000) ... |
17 | β | β | |
18 | β | β | |
The Basilisk | A single line of code that combines all of the above days' solutions into one! |
Currently I am not working on 2022 problems, but this is where I left off:
Day | Part 1 | Part 2 | Commentary |
---|---|---|---|
01 | β | β | |
02 | β | β | |
03 | β | β | |
04 | β | β | |
05 | β | β | This one is a bit cheese but I'm doing my best. Requires Python 3.8 (https://peps.python.org/pep-0572/). |
06 | β | β | |
07 | β | β | Even more cheese. But we got it! |
08 | β | β | Oh boy, I've started cheesing in my OG solutions now too. |
09 | β | β | Today's solution is brought to you by x.insert(0, x.pop(0)+1) . |
10 | β | β | |
11 | β | β | |
12 | β | β | |
13 | β | β | |
14 | β | β | |
The Beast | A single line of code that combines all of the above days' solutions into one! |
Within each year's folder:
the-basilisk
the-beast
day-xx.py
files have my first solution attempts and the resulting one-liners. See them to better understand what the blasphemous one-liners do.I've created some visualizations and memes throughout the month. They can be found in the images folder.
Below is an example vizualization of the length of each day's solution in the Basilisk as of Day 8. I automated creating parts of it; see the tools below.
create-rainbow-line-svg.py
: Creates a rainbow line SVG image from a list of numbers, with each band a calculated percent of the whole. Used to automatically generate the colours and proportions for the visualization above. Also included is a tool for calculating the character counts in my one-line combined solutions, e.g. The Basilisk, or all your solution files in a given year if you provide your first day's file name.create-blank-solution.py
: Creates a new file with a blank solution template for the given year and day. Written explicitly for myself, but feel free to modify it for your personal use if you'd like.import-input.py
: Requests the AoC server for a given day's input and saves it as a new text file. Try to limit your use of this tool to once per day.Here are some fun ways I've been able to convert Python statements that normally require their own line into one-line expressions:
for
and if
statements within a single expression/line.and
and or
to determine the order that expressions are executed, skipping around when necessary.:=
) allows you to create and assign variables within an expression. I discovered it trying to parse Day 5 in 2022, and it has since been a hard carry.a[0] := 1
is invalid). My solution for Day 9 in 2022 was to pop the element, modify it, and then insert it back into the same position, e.g. x.insert(0, x.pop(0)+1)
. Then, for Day 4 in 2023, I discovered the __setitem__()
method, which allows you to update a list element in-line, e.g. x.__setitem__(0, x[0]+1)
. On Day 5 in 2023, I figured out x.__iadd__([0])
is equivalent to x.append(0)
.__import__()
function in 2023 that has finally allowed me to use RegEx in my solutions.while
loop within a list comprehension: True
when it was and included it in the for
condition. But to do this in a single line, I needed to add another if
condition that simply creates and assigns the tracker variable False
, making sure it ran before the main list comprehension was calculated. An example line with a tracker variable called found
: [(x < t[r][c]) or (found:=True) for x in t[r][:c][::-1] if not found] if not (found:=False) else ''
itertools.takewhile(predicate, iterable)
, which solved my while
loop problem in a much cleaner way! Thank goodness I removed my "no import" restriction for this year.Potential employers: I promise my production code is much, much better than this. Please don't blacklist me :(
... but if you're looking to reduce the lines of code in your codebase, I've got some ideas! ;)