optimize 2022/06
This commit is contained in:
parent
c51d56617c
commit
b519084a16
|
@ -1,6 +1,5 @@
|
||||||
#![feature(test, array_windows)]
|
#![feature(test, array_windows)]
|
||||||
extern crate test;
|
extern crate test;
|
||||||
use std::collections::HashSet;
|
|
||||||
|
|
||||||
use aoc2022::{boilerplate, common::*};
|
use aoc2022::{boilerplate, common::*};
|
||||||
|
|
||||||
|
@ -10,23 +9,22 @@ fn parse_input(raw: &str) -> &str {
|
||||||
raw
|
raw
|
||||||
}
|
}
|
||||||
|
|
||||||
// Different implementation because manual comparisons are feasible for groups of 4 and
|
|
||||||
// significantly faster than sets.
|
|
||||||
fn part1(parsed: &str) -> usize {
|
fn part1(parsed: &str) -> usize {
|
||||||
parsed
|
solve::<4>(parsed)
|
||||||
.as_bytes()
|
|
||||||
.array_windows()
|
|
||||||
.zip(4..)
|
|
||||||
.find_map(|([a, b, c, d], i)| (a != b && a != c && a != d && b != c && b != d && c != d).then_some(i))
|
|
||||||
.unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part2(parsed: &str) -> usize {
|
fn part2(parsed: &str) -> usize {
|
||||||
parsed
|
solve::<14>(parsed)
|
||||||
.as_bytes()
|
}
|
||||||
.array_windows::<14>()
|
|
||||||
.zip(14..)
|
fn solve<const BS: usize>(s: &str) -> usize {
|
||||||
.find_map(|(arr, i)| (arr.iter().collect::<HashSet<_>>().len() == 14).then_some(i))
|
s.as_bytes()
|
||||||
|
.array_windows::<BS>()
|
||||||
|
.zip(BS..)
|
||||||
|
.find_map(|(arr, position)| {
|
||||||
|
let bitset = arr.iter().fold(0u32, |bitset, n| bitset | 1 << (n & 0b1_1111));
|
||||||
|
(bitset.count_ones() as usize == BS).then_some(position)
|
||||||
|
})
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user