Cleanup for 2022 day 6: Turned into a lib and introduced Options instead of panicing
This commit is contained in:
parent
040ef7c2fe
commit
c61f28a057
3 changed files with 41 additions and 46 deletions
41
2022/day06-turning_trouble/src/lib.rs
Normal file
41
2022/day06-turning_trouble/src/lib.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
fn find_start_marker(message: &Vec<char>, distinct_character_count: usize) -> Option<usize> {
|
||||||
|
'char_iterator: for index in distinct_character_count-1..message.len() {
|
||||||
|
let mut found: Vec<char> = Vec::with_capacity(distinct_character_count);
|
||||||
|
for offset in 0..distinct_character_count {
|
||||||
|
let this_char = message[index+offset+1-distinct_character_count];
|
||||||
|
if found.contains(&this_char) { continue 'char_iterator; }
|
||||||
|
found.push(this_char);
|
||||||
|
}
|
||||||
|
return Some(index+1);
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(input: &str) -> (Option<usize>, Option<usize>) {
|
||||||
|
let chars = input.chars().collect();
|
||||||
|
let first = find_start_marker(&chars, 4);
|
||||||
|
let second = find_start_marker(&chars, 14);
|
||||||
|
(first, second)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::fs::read_to_string;
|
||||||
|
|
||||||
|
fn read_file(name: &str) -> String {
|
||||||
|
read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sample() {
|
||||||
|
let sample_input = read_file("tests/sample_input");
|
||||||
|
assert_eq!(run(&sample_input), (Some(7), Some(19)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_challenge() {
|
||||||
|
let challenge_input = read_file("tests/challenge_input");
|
||||||
|
assert_eq!(run(&challenge_input), (Some(1702), Some(3559)));
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,46 +0,0 @@
|
||||||
use std::fs;
|
|
||||||
|
|
||||||
fn read_file(path: &str) -> String {
|
|
||||||
fs::read_to_string(path)
|
|
||||||
.expect("File not Found")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn find_start_marker(message: &Vec<char>, distinct_character_count: usize) -> usize {
|
|
||||||
'char_iterator: for index in distinct_character_count-1..message.len() {
|
|
||||||
let mut found: Vec<char> = Vec::with_capacity(distinct_character_count);
|
|
||||||
for offset in 0..distinct_character_count {
|
|
||||||
let this_char = message[index+offset+1-distinct_character_count];
|
|
||||||
if found.contains(&this_char) { continue 'char_iterator; }
|
|
||||||
found.push(this_char);
|
|
||||||
}
|
|
||||||
return index+1;
|
|
||||||
}
|
|
||||||
panic!("No start found");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
//let datastream = read_file("sample_input");
|
|
||||||
let datastream = read_file("input");
|
|
||||||
|
|
||||||
let chars = datastream.chars().collect::<Vec<char>>();
|
|
||||||
println!("Start of Packet: {}", find_start_marker(&chars, 4));
|
|
||||||
println!("Start of Message: {}", find_start_marker(&chars, 14));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn sample_input() {
|
|
||||||
let datastream = read_file("tests/sample_input");
|
|
||||||
|
|
||||||
let chars = datastream.chars().collect::<Vec<_>>();
|
|
||||||
assert_eq!(find_start_marker(&chars, 4), 7);
|
|
||||||
assert_eq!(find_start_marker(&chars, 14), 19);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn challenge_input() {
|
|
||||||
let datastream = read_file("tests/input");
|
|
||||||
|
|
||||||
let chars = datastream.chars().collect::<Vec<_>>();
|
|
||||||
assert_eq!(find_start_marker(&chars, 4), 1702);
|
|
||||||
assert_eq!(find_start_marker(&chars, 14), 3559);
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue