You are reading a single comment by @wence and its replies. Click here to read the full conversation.
  • Bitsets ftw

    #[inline]
    fn unique(s: &[u8], n: usize) -> bool {
        s.iter()
            .fold(0u32, |acc, &c| acc | (1u32.wrapping_shl(c as u32)))
            .count_ones()
            == n as u32
    }
    
    fn solve(inp: &[u8], n: usize) -> usize {
        if let Some((i, _)) = inp.windows(n).enumerate().find(|(_, win)| unique(win, n)) {
            i + n
        } else {
            unreachable!()
        }
    }
    
    pub fn part1(inp: &[u8]) -> usize {
        solve(inp, 14)
    }
    
    pub fn part2(inp: &[u8]) -> usize {
        solve(inp, 14)
    }
    
    
About

Avatar for wence @wence started