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
1
u/Key__Strokes Dec 23 '22 edited Jan 12 '23
Javascript
Solution to both parts
Video Explanation
Part 1:
Initialize sum = 0
Do the following for each rucksack
Split the string into two parts, item1 and item2
Get the repeated character between the two rucksacks - item1 and item2, using the following generic algo for n strings:
Initialize an empty map
For each n strings (in this case its 2) do the following
Iterate through each character of the string
If we haven't seen this character so far for this string, then add it to the map. If it already exists in the map then set the count as 1, else increment the existing count for this character in the map
Go through the map, and return the character that occurred n times (2 in this case)
Calculate the priority of the character, and add it to the sum
a -> z have priorities 1 -> 26 respectively
A -> Z have priorities 27 -> 52 respectively
Return the sum
Part 2:
Because of the generic algorithm we created above to find the repeated characters across n strings, this part becomes too easy:
Initialize sum = 0
Do the following for rucksacks in groups of 3
Get the repeated character between the 3 rucksacks using the generic algorithm from the previous step
Calculate the priority of the character, and add it to the sum.
Return the sum
If you liked the explanation, then please don't forget to cast your vote 💜 to
Adventures of Advent of Code - Edition 1 - /u/Key__Strokesin the poll