diff --git a/.gitignore b/.gitignore index ada8be9..5208977 100644 --- a/.gitignore +++ b/.gitignore @@ -1,14 +1,14 @@ -# Generated by Cargo -# will have compiled files and executables -debug/ -target/ - -# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries -# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html -Cargo.lock - -# These are backup files generated by rustfmt -**/*.rs.bk - -# MSVC Windows builds of rustc generate these, which store debugging information +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information *.pdb \ No newline at end of file diff --git a/day2/src/main.rs b/day2/src/main.rs index a36c66e..b388bba 100644 --- a/day2/src/main.rs +++ b/day2/src/main.rs @@ -14,10 +14,11 @@ fn main() { }) .collect(); - part1(commands) + part1(&commands); + part2(&commands); } -fn part1(commands: Vec<(&str, i32)>) { +fn part1(commands: &Vec<(&str, i32)>) { let mut position = 0; let mut depth = 0; @@ -34,3 +35,23 @@ fn part1(commands: Vec<(&str, i32)>) { } 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); +}