1
0
mirror of https://gitlab.com/MisterBiggs/aoc2021.git synced 2025-06-16 06:56:48 +00:00
AoC2021/day2/rust/src/main.rs
2021-12-02 00:06:22 -07:00

58 lines
1.3 KiB
Rust

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::<i32>().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);
}