1
0
mirror of https://gitlab.com/MisterBiggs/aoc2021.git synced 2025-06-15 22:46:51 +00:00
This commit is contained in:
Anson Biggs 2021-12-01 23:03:53 -07:00
parent 3f4182acf4
commit ece33d6fd3
2 changed files with 36 additions and 15 deletions

26
.gitignore vendored
View File

@ -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

View File

@ -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);
}