[CLEANUP] Removed slower solutions
This commit is contained in:
26
src/day6.rs
26
src/day6.rs
@@ -1,4 +1,3 @@
|
|||||||
use std::collections::HashSet;
|
|
||||||
use crate::day_solver::DaySolver;
|
use crate::day_solver::DaySolver;
|
||||||
|
|
||||||
use super::util;
|
use super::util;
|
||||||
@@ -23,28 +22,7 @@ impl Day6 {
|
|||||||
|
|
||||||
const A_CHAR: usize = 'a' as usize;
|
const A_CHAR: usize = 'a' as usize;
|
||||||
|
|
||||||
// Part 1 took 290 μs on average (Min: 237 μs, Max: 1100 μs, Median: 284 μs)
|
fn are_unique(chars: &[char]) -> bool {
|
||||||
// Part 2 took 1098 μs on average (Min: 792 μs, Max: 1814 μs, Median: 1081 μs)
|
|
||||||
fn are_unique_v1(chars: &[char]) -> bool {
|
|
||||||
return chars.iter().cloned().collect::<HashSet<char>>().len() == chars.len();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Part 1 took 8 μs on average (Min: 7 μs, Max: 53 μs, Median: 8 μs)
|
|
||||||
// Part 2 took 17 μs on average (Min: 15 μs, Max: 83 μs, Median: 17 μs)
|
|
||||||
fn are_unique_v2(chars: &[char]) -> bool {
|
|
||||||
let mut x: u32 = 0;
|
|
||||||
for c in chars {
|
|
||||||
let c_mask = 1 << (c.to_owned() as usize - Day6::A_CHAR);
|
|
||||||
let prev_x = x;
|
|
||||||
x = x | c_mask;
|
|
||||||
if prev_x == x { return false }
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Part 1 took 5 μs on average (Min: 5 μs, Max: 17 μs, Median: 5 μs)
|
|
||||||
// Part 2 took 14 μs on average (Min: 14 μs, Max: 105 μs, Median: 14 μs)
|
|
||||||
fn are_unique_v3(chars: &[char]) -> bool {
|
|
||||||
let mut x: u32 = 0;
|
let mut x: u32 = 0;
|
||||||
for c in chars {
|
for c in chars {
|
||||||
x = x | 1 << (c.to_owned() as usize - Day6::A_CHAR)
|
x = x | 1 << (c.to_owned() as usize - Day6::A_CHAR)
|
||||||
@@ -54,7 +32,7 @@ impl Day6 {
|
|||||||
|
|
||||||
fn find_unique_chain(&self, len: usize) -> Option<usize> {
|
fn find_unique_chain(&self, len: usize) -> Option<usize> {
|
||||||
for i in (len-1)..self.data.len() {
|
for i in (len-1)..self.data.len() {
|
||||||
if Day6::are_unique_v1(&self.data[i-(len - 1)..i+1]) {
|
if Day6::are_unique(&self.data[i-(len - 1)..i+1]) {
|
||||||
return Some(i);
|
return Some(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user