diff --git a/Cargo.lock b/Cargo.lock index d7215f8..919a337 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,3 +65,11 @@ version = "0.1.0" [[package]] name = "d08t2" version = "0.1.0" + +[[package]] +name = "d09t1" +version = "0.1.0" + +[[package]] +name = "d09t2" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 45a65eb..29a8e05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,4 +18,6 @@ members = [ "d07t2", "d08t1", "d08t2", + "d09t1", + "d09t2", ] diff --git a/d09t1/Cargo.toml b/d09t1/Cargo.toml new file mode 100644 index 0000000..c7a8ab6 --- /dev/null +++ b/d09t1/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "d09t1" +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/d09t1/demo_input.txt b/d09t1/demo_input.txt new file mode 100644 index 0000000..70c5595 --- /dev/null +++ b/d09t1/demo_input.txt @@ -0,0 +1,3 @@ +0 3 6 9 12 15 +1 3 6 10 15 21 +10 13 16 21 30 45 \ No newline at end of file diff --git a/d09t1/src/main.rs b/d09t1/src/main.rs new file mode 100644 index 0000000..16d5190 --- /dev/null +++ b/d09t1/src/main.rs @@ -0,0 +1,50 @@ +use std::fs::read_to_string; + +fn main() { + let input = read_to_string("input.txt").unwrap(); + println!("d09t1: {}", d09t1(&input)); +} + +pub fn d09t1(input: &str) -> isize { + input + .lines() + .map(str::split_whitespace) + .map(|line| { + line.map(str::parse::) + .map(Result::unwrap) + .collect::>() + }) + .map(find_next_in_sequence) + .sum() +} + +pub fn find_next_in_sequence(sequence: Vec) -> isize { + let deltas = get_deltas(&sequence); + if *deltas.last().unwrap() == 0 { + *sequence.last().unwrap() + } else { + *sequence.last().unwrap() + find_next_in_sequence(deltas) + } +} + +fn get_deltas(sequence: &Vec) -> Vec { + sequence + .windows(2) + .map(|window| window[1] - window[0]) + .collect::>() +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_d09t1() { + let input = include_str!("../demo_input.txt"); + assert_eq!(d09t1(input), 114); + } + + #[test] + fn test_deltas() { + assert_eq!(get_deltas(&vec![0, 3, 6, 9, 12, 15]), vec![3, 3, 3, 3, 3]) + } +} diff --git a/d09t2/Cargo.toml b/d09t2/Cargo.toml new file mode 100644 index 0000000..34c9aba --- /dev/null +++ b/d09t2/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "d09t2" +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/d09t2/demo_input.txt b/d09t2/demo_input.txt new file mode 100644 index 0000000..70c5595 --- /dev/null +++ b/d09t2/demo_input.txt @@ -0,0 +1,3 @@ +0 3 6 9 12 15 +1 3 6 10 15 21 +10 13 16 21 30 45 \ No newline at end of file diff --git a/d09t2/src/main.rs b/d09t2/src/main.rs new file mode 100644 index 0000000..4fa7034 --- /dev/null +++ b/d09t2/src/main.rs @@ -0,0 +1,56 @@ +use std::fs::read_to_string; + +fn main() { + let input = read_to_string("input.txt").unwrap(); + println!("d09t2: {}", d09t2(&input)); +} + +pub fn d09t2(input: &str) -> isize { + input + .lines() + .map(str::split_whitespace) + .map(|line| { + line.map(str::parse::) + .map(Result::unwrap) + .rev() + .collect::>() + }) + .map(find_next_in_sequence) + .sum() +} + +pub fn find_next_in_sequence(sequence: Vec) -> isize { + let deltas = get_deltas(&sequence); + if deltas.iter().all(|&e| e == 0) { + *sequence.last().unwrap() + } else { + *sequence.last().unwrap() + find_next_in_sequence(deltas) + } +} + +fn get_deltas(sequence: &Vec) -> Vec { + sequence + .windows(2) + .map(|window| window[1] - window[0]) + .collect::>() +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_d09t2() { + let input = include_str!("../demo_input.txt"); + assert_eq!(d09t2(input), 2); + } + + #[test] + fn test_deltas() { + assert_eq!(get_deltas(&vec![0, 3, 6, 9, 12, 15]), vec![3, 3, 3, 3, 3]) + } + + #[test] + fn test_line_1() { + assert_eq!(d09t2("10 13 16 21 30 45"), 5) + } +}