This commit is contained in:
Shadowfacts 2021-12-02 00:08:28 -05:00
parent df0aed7057
commit 084c896615
3 changed files with 1057 additions and 1 deletions

1000
input/day2.txt Normal file

File diff suppressed because it is too large Load Diff

53
src/day2.rs Normal file
View File

@ -0,0 +1,53 @@
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);
}

View File

@ -1,5 +1,8 @@
#![allow(dead_code)]
mod day1;
mod day2;
fn main() {
day1::day1();
day2::day2();
}