Day 2
This commit is contained in:
parent
df0aed7057
commit
084c896615
File diff suppressed because it is too large
Load Diff
|
@ -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);
|
||||
}
|
|
@ -1,5 +1,8 @@
|
|||
#![allow(dead_code)]
|
||||
|
||||
mod day1;
|
||||
mod day2;
|
||||
|
||||
fn main() {
|
||||
day1::day1();
|
||||
day2::day2();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue