diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-06-26 09:47:23 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-06-26 23:37:23 +0200 |
| commit | fa26f694d8cf98272763952a698301ee9935c19e (patch) | |
| tree | 5af7a788fb8e9fa12b2b2fbea64250ed496a23c0 /2022/06 | |
| parent | f0159b7d36e2c81182f2d047a34bf883394894db (diff) | |
| download | aoc-fa26f694d8cf98272763952a698301ee9935c19e.tar.gz aoc-fa26f694d8cf98272763952a698301ee9935c19e.zip | |
Added 2022, first few problems
Diffstat (limited to '2022/06')
| -rw-r--r-- | 2022/06/a.rs | 9 | ||||
| -rw-r--r-- | 2022/06/b.rs | 9 | ||||
| -rw-r--r-- | 2022/06/common.rs | 20 |
3 files changed, 38 insertions, 0 deletions
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 @@ | |||
| 1 | mod common; | ||
| 2 | use common::*; | ||
| 3 | |||
| 4 | fn main() { | ||
| 5 | let mut line = String::new(); | ||
| 6 | std::io::stdin().read_line(&mut line).unwrap(); | ||
| 7 | let i = first_index_n_distinct(line.as_bytes(), 4) + 1; | ||
| 8 | println!("{i}"); | ||
| 9 | } | ||
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 @@ | |||
| 1 | mod common; | ||
| 2 | use common::*; | ||
| 3 | |||
| 4 | fn main() { | ||
| 5 | let mut line = String::new(); | ||
| 6 | std::io::stdin().read_line(&mut line).unwrap(); | ||
| 7 | let i = first_index_n_distinct(line.as_bytes(), 14) + 1; | ||
| 8 | println!("{i}"); | ||
| 9 | } | ||
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 @@ | |||
| 1 | fn all_distinct<T: PartialEq>(a: &[T]) -> bool { | ||
| 2 | for i in 0..a.len() { | ||
| 3 | for j in i+1..a.len() { | ||
| 4 | if a[i] == a[j] { | ||
| 5 | return false; | ||
| 6 | } | ||
| 7 | } | ||
| 8 | } | ||
| 9 | return true; | ||
| 10 | } | ||
| 11 | |||
| 12 | pub fn first_index_n_distinct(a: &[u8], n: usize) -> usize { | ||
| 13 | assert!(n > 0, "{} must be greater than 0", n); | ||
| 14 | for i in 0..a.len()-n+1 { | ||
| 15 | if all_distinct(&a[i..i+n]) { | ||
| 16 | return i+n-1; | ||
| 17 | } | ||
| 18 | } | ||
| 19 | panic!("Cannot find {} distinct in a row", n); | ||
| 20 | } | ||
