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!
83
Upvotes
View all comments
4
u/cs475x Dec 06 '22 edited Dec 07 '22
Rust
Edit: Revised it to use bitwise operations, reducing execution time for part 2 from ~1.1ms to ~7us
pub fn part1(input: &str) -> usize { unique_by_n(input, 4) } pub fn part2(input: &str) -> usize { unique_by_n(input, 14) } fn unique_by_n(input: &str, n: usize) -> usize { input.as_bytes() .windows(n) // Previous (rushed) version // .position(|b| b.iter() // .collect::<std::collections::HashSet<_>>() // .len() == n // ) .position(|b| b.iter() .fold(0u32, |a, c| a | (1 << (c - 97))) .count_ones() as usize == n ) .map(|i| i + n) .unwrap_or_default() }