AoC21/src/day02.rs

54 lines
1.3 KiB
Rust

pub fn day2() {
// let input = r#"
// forward 5
// down 5
// forward 8
// up 3
// down 8
// forward 2
// "#;
let input = include_str!("../input/day2.txt");
let mut x = 0;
let mut d = 0;
for line in input.trim().lines() {
let parts: Vec<&str> = line.trim().split(' ').collect();
let amount: i32 = parts.get(1).unwrap().parse().unwrap();
match *parts.get(0).unwrap() {
"forward" => {
x += amount;
}
"down" => {
d += amount;
}
"up" => {
d -= amount;
}
_ => panic!(),
}
}
println!("{}", x * d);
x = 0;
d = 0;
let mut aim = 0;
for line in input.trim().lines() {
let parts: Vec<&str> = line.trim().split(' ').collect();
let amount: i32 = parts.get(1).unwrap().parse().unwrap();
match *parts.get(0).unwrap() {
"forward" => {
x += amount;
d += aim * amount;
}
"down" => {
aim += amount;
}
"up" => {
aim -= amount;
}
_ => panic!(),
}
}
println!("{}", x * d);
}