From fa26f694d8cf98272763952a698301ee9935c19e Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 26 Jun 2025 09:47:23 +0200 Subject: Added 2022, first few problems --- 2022/06/a.rs | 9 +++++++++ 2022/06/b.rs | 9 +++++++++ 2022/06/common.rs | 20 ++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 2022/06/a.rs create mode 100644 2022/06/b.rs create mode 100644 2022/06/common.rs (limited to '2022/06') diff --git a/2022/06/a.rs b/2022/06/a.rs new file mode 100644 index 0000000..996f9a5 --- /dev/null +++ b/2022/06/a.rs @@ -0,0 +1,9 @@ +mod common; +use common::*; + +fn main() { + let mut line = String::new(); + std::io::stdin().read_line(&mut line).unwrap(); + let i = first_index_n_distinct(line.as_bytes(), 4) + 1; + println!("{i}"); +} diff --git a/2022/06/b.rs b/2022/06/b.rs new file mode 100644 index 0000000..78d2703 --- /dev/null +++ b/2022/06/b.rs @@ -0,0 +1,9 @@ +mod common; +use common::*; + +fn main() { + let mut line = String::new(); + std::io::stdin().read_line(&mut line).unwrap(); + let i = first_index_n_distinct(line.as_bytes(), 14) + 1; + println!("{i}"); +} diff --git a/2022/06/common.rs b/2022/06/common.rs new file mode 100644 index 0000000..ed5360c --- /dev/null +++ b/2022/06/common.rs @@ -0,0 +1,20 @@ +fn all_distinct(a: &[T]) -> bool { + for i in 0..a.len() { + for j in i+1..a.len() { + if a[i] == a[j] { + return false; + } + } + } + return true; +} + +pub fn first_index_n_distinct(a: &[u8], n: usize) -> usize { + assert!(n > 0, "{} must be greater than 0", n); + for i in 0..a.len()-n+1 { + if all_distinct(&a[i..i+n]) { + return i+n-1; + } + } + panic!("Cannot find {} distinct in a row", n); +} -- cgit v1.3