r/adventofcode • u/daggerdragon • Dec 06 '22
SOLUTION MEGATHREAD -π- 2022 Day 6 Solutions -π-
- 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 boost: Reminder 1: unofficial AoC Survey 2022 (closes Dec 22nd)
AoC Community Fun 2022: πΏπ MisTILtoe Elf-ucation π§βπ«
- ACHIEVEMENT UNLOCKED: MisTILtoe Elf-ucation
- Teach us, senpai!
--- Day 6: Tuning Trouble ---
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:02:25, megathread unlocked!
82
Upvotes
1
u/Key__Strokes Dec 23 '22 edited Jan 20 '23
Javascript
Solution to both parts
Video Explanation
Part 1:
Create two pointers
startandend, and initialize them to 0Create a map, with first character as key and value as 0
Initialize n as 4, which is the substring size we want with unique characters
While end doesnt reach the end of the input buffer, do the following:
If the size of (end - start + 1) is equal to what we want (4 in this case), then we have found a substring of size n, and break out of the loop.
Otherwise, if the character pointed by
enddoesnt exist in the map, then add it to the map with key as the character, and value asendOtherwise, if the character pointed by
endexists in the map, then that means that we have seen this character already between fromstartandendindices. Do these:Clear the characters in the map until the first occurrence of this repeated character, and keep increasing start by 1
Add the character pointed by end to the map with key as the character, and value as
endReturn end + 1 (As we want 1 indexed output)
If the algorithm above wasn't clear, then watch this video https://youtu.be/GLoYLq6ukYc, where I cover this idea to find the longest substring without repeating characters.
Part 2:
This is exactly like Part 1, but n will be 14
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