r/adventofcode • • Dec 06 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 6 Solutions -πŸŽ„-


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.


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!

83 Upvotes

1.8k comments sorted by

View all comments

2

u/Dutchcheesehead Dec 06 '22 edited Dec 14 '22

Python
Really seemed like a simple puzzle. Did not even need CoPilot to help me solve it: https://youtu.be/owEQXpeoh1k.I do wonder how people did who don't know about sets...

Code here

1

u/daggerdragon Dec 06 '22 edited Dec 14 '22

Post removed. Top-level posts in Solution Megathreads are for code solutions only.

Edit your post to share your text code/repo/solution. The video can stay, but we need the code too.

While you're at it, state which programming language this code is written in. This makes it easier for folks who Ctrl-F the megathreads looking for a specific language.

If you fix your post to include the required items, I'll re-approve your post.

Edit: thanks adding your code! Re-approved.

2

u/Dutchcheesehead Dec 14 '22

Hey, I added the code as a topaz-paste link. Let me know if this is good enough!

1

u/B3tal Dec 06 '22

I do wonder how people did who don't know about sets...

Without Sets the simplest solution I imagine people would come up with would just be two nested loops, that check each pair of characters in the range for duplicates. Given the relatively small range you need to check this still isn't that big of a problem performance wise

1

u/Bigluser Dec 06 '22

The semi smart solution would be to sort the 14 character window and then compare if two consecutive characters are equal.

2

u/Dutchcheesehead Dec 06 '22 edited Dec 06 '22

That is actually quite smart! Didn't think about that one yet!

Edit: here is the code :)

def start_of_marker(line, num_chars):    
for end in range(num_chars, len(line)):
    start = end - num_chars
    window = sorted(line[start:end])
    is_unique = True
    for i in range(num_chars-1):
        if window[i] == window[i+1]:
            is_unique = False
            break
    if is_unique:
        return end
raise ValueError('No marker found')

print('part 1', start_of_marker(line, 4)) print('part 2', start_of_marker(line, 14))

1

u/French__Canadian Dec 06 '22

The really smart solution is to use a hash map.

1

u/Dutchcheesehead Dec 06 '22
from collections import Counter

def start_of_marker(line, num_chars):
count = Counter() for i, char in enumerate(line): count[char]+=1 if i >= num_chars: count[line[i-num_chars]]-=1 # Remove the char that is no longer in the window assert sum(count.values()) == num_chars

        if max(count.values()) == 1: # If all unique the highest count is 1
            return i+1
raise ValueError('No marker found')

print('part 1', start_of_marker(line, 4)) print('part 2', start_of_marker(line, 14))

I implemented it with a counter as well now :)
Should be single-pass O(n) (depending on how max(count.values()) is implemented...

1

u/Boojum Dec 06 '22

I used sets, myself.

But if I didn't have them and were using a lower-level language like C, I might just keep an array of counters per letter, and a counter of letter-counters that are over one. Add one to the letter shifting in on the right (and increment the counter-counter if the letter counter goes from 1 to 2), then subtract one from the letter shifting out on the right (and decrement the counter-counter if the letter counter goes from 2 to 1). If the counter-counter hits zero, we're done. Nice and linear at O(n) time-complexity.

Basically something like this (untested) for the main loop:

int lc[ 26 ] = { 0 };
int cc = 0;
for ( int i = 0; i < msglen; ++i ) {
    if ( ++lc[ msg[ i ] - 'a' ] == 2 )
        ++cc;
    if ( i >= 14 && --lc[ msg[ i - 14 ] - 'a' ] == 1 )
        if ( --cc == 1 )
            break;
}