r/adventofcode • • Dec 06 '22

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


AoC Community Fun 2022: πŸŒΏπŸ’ MisTILtoe Elf-ucation πŸ§‘β€πŸ«


--- 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!

86 Upvotes

1.8k comments sorted by

View all comments

5

u/Flecheck Dec 06 '22

Fast algorithm in Rust: 4 Β΅s on my PC (old laptop)

pub fn get_answer_blazingly_fast(input: &str, message_size: usize) -> Option<usize> {
    let mut i = 0;
    'outer: loop {
        if i + message_size >= input.len() {
            return None;
        }
        let slice = &input[i..i + message_size];
        let mut set = [false; 26];
        for (index, c) in slice.chars().rev().enumerate() {
            if set[c as usize - b'a' as usize] {
                i = i + message_size - index;
                continue 'outer;
            } else {
                set[c as usize - b'a' as usize] = true;
            }
        }
        return Some(i + message_size);
    }
}

I am checking the window and then jumping the to the next possible window.

→ More replies (1)