Day - part 1 done hehehe
This commit is contained in:
parent
9a050b663d
commit
6a8ea1b392
@ -13,4 +13,3 @@ rpath = true
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
nom = "7.1.1"
|
nom = "7.1.1"
|
||||||
itertools = "0.10.5"
|
itertools = "0.10.5"
|
||||||
fancy-regex = { version = "0.10.0", features = ["perf-literal", "perf-dfa", "perf-inline", "perf-cache"] }
|
|
||||||
|
60
src/prob6.rs
60
src/prob6.rs
@ -1,57 +1,13 @@
|
|||||||
use std::collections::HashSet;
|
use itertools::Itertools;
|
||||||
|
|
||||||
use fancy_regex::Regex;
|
fn begin_datagram(datagram: &str, size: usize) -> usize {
|
||||||
|
size + datagram
|
||||||
/// Tries to find the 4 character match.
|
.as_bytes()
|
||||||
///
|
.windows(size)
|
||||||
/// # Arguments
|
.position(|window| window.iter().tuple_combinations().all(|(a, b)| a != b))
|
||||||
///
|
.unwrap()
|
||||||
/// * `datagram`:
|
|
||||||
///
|
|
||||||
/// returns: ()
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
fn begin_datagram(datagram: &str) {
|
|
||||||
let mut letters = HashSet::<char>::new();
|
|
||||||
let primary_iter = datagram.chars().fuse();
|
|
||||||
let mut motif_iter = primary_iter.clone();
|
|
||||||
while letters.len() < 4 {
|
|
||||||
if let Some(tmp) = motif_iter.next() {
|
|
||||||
if !letters.insert(tmp) {
|
|
||||||
letters.clear();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn solve_part1(content: &str) -> i32 {
|
pub fn solve_part1(content: &str) -> i32 {
|
||||||
let _content = content;
|
begin_datagram(content, 4) as i32
|
||||||
return match Regex::new(r"^(?:([A-Za-z])(?!.*\1))*$") {
|
|
||||||
Ok(_reg) => {
|
|
||||||
println!("Regex is built.");
|
|
||||||
match _reg.find(_content) {
|
|
||||||
Ok(matches) => {
|
|
||||||
if let Some(yay) = matches {
|
|
||||||
println!("{}", yay.as_str());
|
|
||||||
} else {
|
|
||||||
println!("Snif no match :(");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
eprintln!("{err}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
0
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
eprintln!("{err}");
|
|
||||||
0
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user