Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🐍📅 One-line Python solutions for Advent of Code 2022 and 2023.

NotificationsYou must be signed in to change notification settings

savbell/advent-of-code-one-liners

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

As a personal challenge, I'm trying to solve everyAdvent 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 throughmy Reddit posts.

Note that per copyright andEric 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 version3.11.3. The solutions should not require any additional packages (I'm only importing from thePython Standard Library), but in thetools folder, I have a script for importing the input files from the Advent of Code server that usesrequests and a visualization tool that usesmatplotlib andsvgwrite.

Progress Tracking

StatusDescription
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

2023 Solutions

DayPart 1Part 2Commentary
01This year, I'm removing my "no imports" restriction 😅
02Getting in a lot of RegEx practice this year!
03Oh boy, the amount of RegEx I'm using is slowing down my computer... andthe Walrus is back!
04Pulling out somedunder (magic) methods for this one!
05☑️lambdas are making their first appearance... And credit toDylan Gray for his immense help with my Part 2 OG solution!
06reduce has joined the fray!
07First solution I didn't use RegEx... there goes that challenge for myself 😂
08itertools.takewhile is a lifesaver!
09Gonna be a busy few days so I might fall behind for a bit...
10Not sure why this one took me until Day 17 to solve, but we finally did it! Proud of my Part 2 😁
11So glad I didn't need to modify my approach for Part 2!
12☑️Credit toSøren Fuglede Jørgensen for their solution for Part 2!
13I did go back and rewrite Part 1 after solving Part 2. This one took me a while for some reason!
14The hardest part is always thewhile loops!
15My fastest solve yet!
16Let's just ignore thesetrecursionlimit(30000)...
17
18
The BasiliskA single line of code that combines all of the above days' solutions into one!

2022 Solutions

Currently I am not working on 2022 problems, but this is where I left off:

DayPart 1Part 2Commentary
01
02
03
04
05This one is a bit cheese but I'm doing my best. Requires Python 3.8 (https://peps.python.org/pep-0572/).
06
07Even more cheese. But we got it!
08Oh boy, I've started cheesing in my OG solutions now too.
09Today's solution is brought to you byx.insert(0, x.pop(0)+1).
10
11
12
13
14
The BeastA single line of code that combines all of the above days' solutions into one!

Repo Organization

Solutions

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:
  • Theday-xx.py files have my first solution attempts and the resulting one-liners. See them to better understand what the blasphemous one-liners do.

Images

I've created some visualizations and memes throughout the month. They can be found in theimages 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.

A snake with rainbow bands where each colour corresponds to how many characters were used to solve each Advent of Code problem

Tools

  • 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 tolimit your use of this tool to once per day.

Some Strategy Explanations

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'slist comprehensions, which allows you to havefor andif statements within a single expression/line.
  • Short-circuit (lazy) evaluation: Python hasshort-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 useand andor to determine the order that expressions are executed, skipping around when necessary.
  • In-line assignment operator: TheWalrus operator (:=) allows you to create and assign variables within an expression. I discovered it trying to parseDay 5 in 2022, and it has since beena 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 forDay 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, forDay 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). OnDay 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 useRegEx in my solutions.
  • Mywhile loop within a list comprehension:I was very proud of discovering this while working on2022'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 solving2023'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.

Concluding Notes

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! ;)

About

🐍📅 One-line Python solutions for Advent of Code 2022 and 2023.

Topics

Resources

Stars

Watchers

Forks

Languages


[8]ページ先頭

©2009-2025 Movatter.jp