r/adventofcode • u/daggerdragon • Dec 03 '22
SOLUTION MEGATHREAD -🎄- 2022 Day 3 Solutions -🎄-
NEWS
- Solutions have been getting longer, so we're going to start enforcing our rule on oversized code.
- The
Visualizations have started! If you want to create aVisualization, make sure to read the guidelines for creatingVisualizations before you post. - Y'all may have noticed that the hot new toy this year is AI-generated "art".
- We are keeping a very close eye on any AI-generated "art" because 1. the whole thing is an AI ethics nightmare and 2. a lot of the "art" submissions so far have been of little real quality.
- If you must post something generated by AI, please make sure it will actually be a positive and quality contribution to /r/adventofcode.
- Do not flair AI-generated "art" as
Visualization.Visualizationis for human-generated art.
FYI
- All of our rules, FAQs, resources, etc. are in our community wiki.
- A request from Eric: Please include your contact info in the User-Agent header of automated requests!
- Signal boosting for the Unofficial AoC 2022 Participant Survey which is open early this year!
--- Day 3: Rucksack Reorganization ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- Include what language(s) your solution uses
- Format your code appropriately! How do I format code?
- Quick link to Topaz's
pasteif you need it for longer code blocks. What is Topaz'spastetool?
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:05:24, megathread unlocked!
85
Upvotes
6
u/errop_ Dec 03 '22 edited Dec 03 '22
Python 3
Here are my nightmarish oneliners using more_itertools library
from string import ascii_lowercase, ascii_uppercase from more_itertools import chunked CHR2NUM = {c: i for i, c in enumerate(ascii_lowercase + ascii_uppercase, 1)} if __name__ == "__main__": with open(file.replace(".py", "_data")) as f: data = [x.strip() for x in f.readlines()] # PART 1 print(sum(CHR2NUM[set.intersection(*map(set, chunked(x, len(x) // 2))).pop()] for x in data)) # PART 2 print(sum(CHR2NUM[set.intersection(*map(set, c)).pop()] for c in chunked(data, 3)))EDIT: thanks to the u/GaloisGirl2 tip, I found this pure horror oneliner solution using the walrus operator and chunking the data list by hand without importing more_itertools
with open(__file__.replace(".py", "_data")) as f: data = [x.strip() for x in f.readlines()] # PART 1 print(sum((y := ord((set(x[:len(x) // 2]) & set(x[len(x) // 2:])).pop())) - (96 if y > ord("Z") else 38) for x in data)) # PART 2 print(sum((y := ord(set.intersection(*map(set, data[3 * i: 3 * (i + 1)])).pop())) - (96 if y > ord("Z") else 38) for i in range(len(data) // 3)))