diff --git a/day2/Cargo.toml b/day2/Cargo.toml deleted file mode 100644 index 659d7e3..0000000 --- a/day2/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "day2" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -serde = "1.0.130" diff --git a/day2/rust/src/main.rs b/day2/rust/src/main.rs index e7a11a9..b388bba 100644 --- a/day2/rust/src/main.rs +++ b/day2/rust/src/main.rs @@ -1,3 +1,57 @@ +use std::fs; + fn main() { - println!("Hello, world!"); + let input = fs::read_to_string("./input.txt").expect("Could not read file"); + + let commands: Vec<(&str, i32)> = input + .lines() + .map(|s| { + let mut split = s.split_whitespace(); + ( + split.next().unwrap(), + split.next().unwrap().parse::().unwrap(), + ) + }) + .collect(); + + part1(&commands); + part2(&commands); +} + +fn part1(commands: &Vec<(&str, i32)>) { + let mut position = 0; + let mut depth = 0; + + for (dir, amt) in commands.iter().map(|a| *a) { + let change = match dir { + "forward" => (amt, 0), + "up" => (0, -amt), + "down" => (0, amt), + _ => (0, 0), + }; + + position += change.0; + depth += change.1; + } + println!("Part 1: {}", position * depth); +} + +fn part2(commands: &Vec<(&str, i32)>) { + let mut position = 0; + let mut depth = 0; + let mut aim = 0; + + for (dir, amt) in commands.iter().map(|a| *a) { + let change = match dir { + "forward" => (amt, 0), + "up" => (0, -amt), + "down" => (0, amt), + _ => (0, 0), + }; + + aim += change.1; + position += change.0; + depth += aim * change.0; + } + println!("Part 2: {}", position * depth); } diff --git a/day2/src/main.rs b/day2/src/main.rs deleted file mode 100644 index b388bba..0000000 --- a/day2/src/main.rs +++ /dev/null @@ -1,57 +0,0 @@ -use std::fs; - -fn main() { - let input = fs::read_to_string("./input.txt").expect("Could not read file"); - - let commands: Vec<(&str, i32)> = input - .lines() - .map(|s| { - let mut split = s.split_whitespace(); - ( - split.next().unwrap(), - split.next().unwrap().parse::().unwrap(), - ) - }) - .collect(); - - part1(&commands); - part2(&commands); -} - -fn part1(commands: &Vec<(&str, i32)>) { - let mut position = 0; - let mut depth = 0; - - for (dir, amt) in commands.iter().map(|a| *a) { - let change = match dir { - "forward" => (amt, 0), - "up" => (0, -amt), - "down" => (0, amt), - _ => (0, 0), - }; - - position += change.0; - depth += change.1; - } - println!("Part 1: {}", position * depth); -} - -fn part2(commands: &Vec<(&str, i32)>) { - let mut position = 0; - let mut depth = 0; - let mut aim = 0; - - for (dir, amt) in commands.iter().map(|a| *a) { - let change = match dir { - "forward" => (amt, 0), - "up" => (0, -amt), - "down" => (0, amt), - _ => (0, 0), - }; - - aim += change.1; - position += change.0; - depth += aim * change.0; - } - println!("Part 2: {}", position * depth); -}