diff --git a/2022/src/bin/06.rs b/2022/src/bin/06.rs new file mode 100644 index 0000000..8dd2d9b --- /dev/null +++ b/2022/src/bin/06.rs @@ -0,0 +1,58 @@ +use std::collections::HashSet; + +// type opcode<'a> = (usize, &'a str); + +fn get_opcodes(input: &str, len: usize) -> Vec { + let mut ops: Vec = Vec::new(); + + for i in len..input.len() { + let op: Vec = input[i-len..i].chars().collect(); + let setop: HashSet = HashSet::from_iter(op.iter().copied()); + + // println!("{len}: {i} {op:?}"); + + if setop.len() == len { + ops.push(i); + // println!("\t pog"); + } + } + + ops +} + +pub fn part_one(input: &str) -> Option { + let cringe = get_opcodes(input, 4); + let first = *cringe.first().unwrap(); + + Some(first as u32) +} + +pub fn part_two(input: &str) -> Option { + let cringe = get_opcodes(input, 14); + let first = *cringe.first().unwrap(); + + Some(first as u32) +} + +fn main() { + let input = &advent_of_code::read_file("inputs", 6); + advent_of_code::solve!(1, part_one, input); + advent_of_code::solve!(2, part_two, input); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part_one() { + let input = advent_of_code::read_file("examples", 6); + assert_eq!(part_one(&input), Some(7)); + } + + #[test] + fn test_part_two() { + let input = advent_of_code::read_file("examples", 6); + assert_eq!(part_two(&input), Some(19)); + } +} diff --git a/2022/src/examples/06.txt b/2022/src/examples/06.txt new file mode 100644 index 0000000..5a2b0a7 --- /dev/null +++ b/2022/src/examples/06.txt @@ -0,0 +1 @@ +mjqjpqmgbljsphdztnvjfqwrcgsmlb \ No newline at end of file