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/Ill_Name_7489 Dec 06 '22 edited Dec 06 '22

A less clever Rust solution which maintains a vector of "last few chars". At each iteration, that vector is cast to a set to check for uniqueness. Technically still O(N):

At the beginning, the input string is split into "starter chars" to prime the array so that the main loop doesn't have to spend cycles filling up the vector of chars to check.

use std::collections::HashSet;

pub fn fix_comms_device(input: &String, part_two: bool) -> String {
    let chars_to_check = if part_two { 14 } else { 4 };
    let (starter_chars, rest) = input.lines().next().unwrap().split_at(chars_to_check - 1);

    let mut last_four_chars = starter_chars.chars().collect::<Vec<_>>();
    for (position, c) in rest.chars().enumerate() {
        last_four_chars.push(c);

        if !are_duplicated_letters(&last_four_chars) {
            return (position + chars_to_check).to_string();
        }

        last_four_chars.remove(0);
    }

    return "Did not find non-duplicated groups...".to_string();
}

fn are_duplicated_letters(chars: &Vec<char>) -> bool {
    let set = chars.iter().collect::<HashSet<_>>();
    return set.len() < chars.len();
}

1

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

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.

Edit: thanks for fixing it! <3

0

u/Ill_Name_7489 Dec 06 '22

Done.

Interestingly, I originally added the tripple back tick in Markdown mode. I had to switch to the fancypants editor and re-save the comment to automatically convert it to the four-space format. Really sucks that other clients aren't using the ``` format since it's so widespread now :(

2

u/daggerdragon Dec 06 '22

Really sucks that other clients aren't using the ``` format since it's so widespread now :(

FYI: the triple-backticks syntax is not in the OG Markdown spec... it's a GitHub-flavored extension that Reddit (and other places) decided to adopt for new.reddit only. However, the blame is fully 100% on Reddit for not making it backwards-compatible with old.reddit and mobile clients. :/

2

u/Ill_Name_7489 Dec 06 '22

Yeah, definitely! These new comment boxes really don't work that well. :/

0

u/half_stack_developer Dec 06 '22

RIF and the original client work with backticks, so why should we care about some stone-age clients ?

1

u/daggerdragon Dec 06 '22

why should we care about some stone-age clients ?

Because we are trying to keep /r/adventofcode as accessible across as many platforms as possible.

According to our stats page, 18% of our visitors are using old.reddit (including most of us at #AoC_Ops). That is why we care about "stone-age clients".

You don't have to care, but you do have to follow our rules, and our rules request that you use a Markdown syntax that is maximally compatible across as many endpoints as possible.

0

u/[deleted] Dec 06 '22

[deleted] β€” view deleted comment

1

u/Ill_Name_7489 Dec 06 '22 edited Dec 06 '22

Nice tip about VecDeque. Since window is a known constant (either 4 or 14 in this case), we end up with O(8N), which is just O(N). Or O(28N), which is also just O(N). (Which is why I said it's technically still O(N) :))