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:
- All the one-line solutions are combined into a single disgusting line of code that solves all the Advent of Code problems at once, nicknamed based on the year:
- 2023:
the-basilisk
- 2022:
the-beast
- 2023:
- The
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:
- List comprehensions: This entire thing wouldn't be possible without Python's list comprehensions, which allows you to have
for
andif
statements within a single expression/line. - Short-circuit (lazy) evaluation: Python has short-circuit evaluation for its Boolean operations, which means the second expression in a Boolean operation is only evaluated if the result of the first expression does not guarantee the final result of the expression. This allows me to strategically use
and
andor
to determine the order that expressions are executed, skipping around when necessary. - In-line assignment operator: The Walrus operator (
:=
) 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. - Updating a list element in-line: Since the assignment operator cannot be used with subscripts, it cannot be used to update lists (e.g.
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 outx.__iadd__([0])
is equivalent tox.append(0)
. - In-line import statements: After not using import statements in 2022 since it would technically make my solutions more than one line, I discovered the built-in
__import__()
function in 2023 that has finally allowed me to use RegEx in my solutions. - My
while
loop within a list comprehension:I was very proud of discovering this while working on 2022's Day 8. Since I wanted my list comprehension to end execution once a condition was met, I had a tracker variable turnTrue
when it was and included it in thefor
condition. But to do this in a single line, I needed to add anotherif
condition that simply creates and assigns the tracker variableFalse
, making sure it ran before the main list comprehension was calculated. An example line with a tracker variable calledfound
:[(x < t[r][c]) or (found:=True) for x in t[r][:c][::-1] if not found] if not (found:=False) else ''
Update: While solving 2023's Day 8, I discovereditertools.takewhile(predicate, iterable)
, which solved mywhile
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! ;)