From 350002299370477a0b25dc6506e178f7c1ef8bf7 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 3 May 2023 15:07:25 +0200 Subject: Add day 6 --- 2022/day6/src/lib.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 2022/day6/src/lib.rs (limited to '2022/day6/src') diff --git a/2022/day6/src/lib.rs b/2022/day6/src/lib.rs new file mode 100644 index 0000000..76acb53 --- /dev/null +++ b/2022/day6/src/lib.rs @@ -0,0 +1,67 @@ +pub fn part(input: &str, m: usize) -> usize { + let n = input.len(); + + for i in 0..n { + let s = &input[i..i + m]; + let mut check = false; + if s.trim().len() < m { + break; + } + + 'o: for j in 0..m { + for k in 0..m { + if j == k { + continue; + } + + if s.chars().nth(j) == s.chars().nth(k) { + check = true; + break 'o; + } + } + } + + if !check { + return i + m; + } + } + + 0 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_example() { + let input = include_str!("../example.txt"); + let result = part(input, 4); + + assert_eq!(result, 5); + } + + #[test] + fn test_input() { + let input = include_str!("../input.txt"); + let result = part(input, 4); + + assert_eq!(result, 1779); + } + + #[test] + fn test_example2() { + let input = include_str!("../example.txt"); + let result = part(input, 14); + + assert_eq!(result, 23); + } + + #[test] + fn test_input2() { + let input = include_str!("../input.txt"); + let result = part(input, 14); + + assert_eq!(result, 23); + } +} -- cgit v1.2.3-18-g5258