As a personal challenge, I'm trying to solve every Advent of Code problem in a single line of Python code. I've successfully managed to solve Days 1-7 so far in the most disgusting line of code I've ever written, affectionately called The Beast. Feel free to follow along on my GitHub where I've uploaded each individual day's progress!
Thank you! I won't lie, I felt a bit giddy once I figured out how to do a while loop within a list comprehension.
"e" used to be called "found" and is initially set to False, for anyone confused — I wanted to count the number of elements that were less than t[r][c] but stop after I found the first one greater than or equal to it. In this particular case, I wanted to also count the element that was greater than/equal to, hence why "e" starts as False and is later set to True rather than the other way around.
Quote from a friend when I showed him this: "i pasted that file into vscode to run Format Document on it and it did literally nothing. autopep8 looked at this and said "what do you mean chief this looks fine to me""
Thank you! I'm oddly proud of my creation. Bet I feel similarly to Dr. Frankenstein after he made his own monster ;)
I'm curious too! I didn't realize how many people would see this, so now the pressure is really on... I really don't know how long I'll last, but I'm going to do my best! Day 8 was another success at least! :D
map(int,g) # instead of [int(x) for x in g]
map({'A X':3,'A Y':4,'A Z':8,'B X':1,'B Y':5,'B Z':9,'C X':2,'C Y':6,'C Z':7}.get,open(q[2]).read().split('\n')) # [{'A X':3,'A Y':4,'A Z':8,'B X':1,'B Y':5,'B Z':9,'C X':2,'C Y':6,'C Z':7}[x] for x in open(q[2]).read().split('\n')] # Saves recreating the dict each loop
and you can use generator comprehensions instead of list comprehensions to save a little bit of memory (if that even matters!)
max(blah for thing in stuff) # max([blah for thing in stuff])
max(blah for thing in (line.split(" ") for line in open(file))) # max([blah for thing in [line.split(" ") for line in open(file)]])
and you could use a negative slice for day 1 part 2 instead of reversing the sorted list: sorted(...)[-3:]
Overall, that is incredible and slightly horrifying - I love it!
Thank you so much for the tips! I'm definitely learning a lot of new things every day. I'm already looking back at the lines I've previously written and thinking about ways to "optimize" them (if this code could ever be considered optimal under any definition!), but we'll see if I end up having the time for that — maybe after the event ends! For now, I'll try and see where I can apply these to future problems — thanks! :)
I'm learning a ton! But trust me, it isn't anything you'd want to bring out at an actual job — that is, if you want to keep said job!
In all seriousness, if people are interested then I might do a write-up for the MisTILtoe Elf-ucation event where I explain some of my logic, like my while loop.
Ha, I wish I could claim that the amount of time I spent on each problem is directly proportional to how many lines of code my solution is... How about let's just pretend that's true ;)
Python is even a lot faster than that most of the time. For a lot of things it's as fast as any language. There are only a couple kinds of projects where I really feel a need to use a faster language.
77
u/ImpossibleSav Dec 08 '22 edited Dec 08 '22
As a personal challenge, I'm trying to solve every Advent of Code problem in a single line of Python code. I've successfully managed to solve Days 1-7 so far in the most disgusting line of code I've ever written, affectionately called The Beast. Feel free to follow along on my GitHub where I've uploaded each individual day's progress!
Edit: Day 8's in there now too! :)
13
u/_ashika__ Dec 08 '22
I'm rooting for you, internet stranger
12
u/matejcik Dec 08 '22
I originally didn't want to believe that it's a single line for all the puzzles at the same time.
It's a thing of beauty, I'm curious how long you can keep this up!
8
u/AllanTaylor314 Dec 08 '22
I reckon
mapwould be useful in places e.g.and you can use generator comprehensions instead of list comprehensions to save a little bit of memory (if that even matters!)
and you could use a negative slice for day 1 part 2 instead of reversing the sorted list:
sorted(...)[-3:]Overall, that is incredible and slightly horrifying - I love it!
4
u/supergnaw Dec 08 '22
This is disgusting and I'm here for it lol.
3
u/typingmonk Dec 08 '22
Beside of having fun, I wonder what can I learn from coding it in only one single line.
2
u/CMDRKeyfox Dec 09 '22
"Restructure and simplify code."
--Sourcery - 2022
0
u/[deleted] Dec 08 '22
[deleted] — view deleted comment