-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday_02.rs
45 lines (36 loc) · 954 Bytes
/
day_02.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use common::{solution, Answer};
solution!("Dive!", 2);
fn part_a(input: &str) -> Answer {
let mut dep: u32 = 0;
let mut hor: u32 = 0;
for i in input.lines() {
let seg = i.split(' ').collect::<Vec<&str>>();
let x = seg[1].parse::<u32>().unwrap();
match seg[0] {
"forward" => hor += x,
"up" => dep -= x,
"down" => dep += x,
_ => {}
}
}
(dep * hor).into()
}
fn part_b(input: &str) -> Answer {
let mut dep: u32 = 0;
let mut hor: u32 = 0;
let mut aim: u32 = 0;
for i in input.lines() {
let seg = i.split(' ').collect::<Vec<&str>>();
let x = seg[1].parse::<u32>().unwrap();
match seg[0] {
"forward" => {
hor += x;
dep += aim * x;
}
"up" => aim -= x,
"down" => aim += x,
_ => {}
}
}
(dep * hor).into()
}