diff --git a/d6/Cargo.lock b/d6/Cargo.lock new file mode 100644 index 0000000..23dadee --- /dev/null +++ b/d6/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "d6" +version = "0.1.0" diff --git a/d6/Cargo.toml b/d6/Cargo.toml new file mode 100644 index 0000000..091e72b --- /dev/null +++ b/d6/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "d6" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/d6/src/main.rs b/d6/src/main.rs new file mode 100644 index 0000000..11fa5eb --- /dev/null +++ b/d6/src/main.rs @@ -0,0 +1,55 @@ +use std::{fs::read_to_string, io::Result}; + +fn all_diff(list: &[T]) -> bool { + for i in 0..list.len() { + let a = &list[i]; + for j in 0..i { + let b = &list[j]; + if a == b { + return false; + } + } + } + true +} + +fn main() -> Result<()> { + let mut p1_ring_buf = [None; 4]; + let mut p1_pos = 0; + let mut p2_ring_buf = [None; 14]; + let mut p2_pos = 0; + + let mut p1_result = 0; + let mut p2_result = 0; + + for (i, c) in read_to_string("input.txt")?.chars().enumerate() { + p1_ring_buf[p1_pos] = Some(c); + p1_pos += 1; + if p1_pos == p1_ring_buf.len() { + p1_pos = 0 + } + + p2_ring_buf[p2_pos] = Some(c); + p2_pos += 1; + if p2_pos == p2_ring_buf.len() { + p2_pos = 0 + } + + if p1_result == 0 { + if i >= p1_ring_buf.len() && all_diff(&p1_ring_buf) { + p1_result = i + 1; + } + } + + if p2_result == 0 { + if i >= p2_ring_buf.len() && all_diff(&p2_ring_buf) { + p2_result = i + 1; + } + } + } + + println!("Part 1: {}", p1_result); + println!("Part 2: {}", p2_result); + + Ok(()) +}