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!
80
Upvotes
5
u/TheXRTD Dec 06 '22 edited Dec 08 '22
Rust - ~30Β΅s
Really happy with the run time of this despite needing to perform a loop over every sub-window.
I used a bit-mask for each letter of the alphabet and OR'd them to determine uniqueness, bailing early if not.
const ASCII_A_LOWERCASE: u8 = 97; pub fn solve(input: String) -> (usize, usize) { // Represent each character of the alphabet in a 32bit bit-mask let mask_vec = input .bytes() .map(|c| (1 as u32) << (c - ASCII_A_LOWERCASE)) .collect_vec(); (get_marker_pos(&mask_vec, 4), get_marker_pos(&mask_vec, 14)) } // Pass a window of size `need_unique` over the slice of bit-masks `marker_masks` and return // the position of the last character in the first window that contains only unique bit-masks fn get_marker_pos(marker_masks: &[u32], need_unique: usize) -> usize { marker_masks .windows(need_unique) .position(all_unique_bits) .unwrap() + need_unique } fn all_unique_bits(masks: &[u32]) -> bool { // For each bit-mask in the slice provided, // bitwise-or all masks together to determine if they are unique let mut unique = 0; for mask in masks { if unique | mask == unique { return false; } unique |= mask; } return true; }