-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-2.rs
60 lines (53 loc) · 1.39 KB
/
day-2.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::str::FromStr;
const INPUT: &str = include_str!("day-2.input");
enum Command {
Forward(isize),
Down(isize),
Up(isize),
}
impl FromStr for Command {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut s = s.split_ascii_whitespace();
let cmd = s.next().unwrap();
let n = s.next().unwrap().parse::<isize>().unwrap();
Ok(match cmd {
"forward" => Command::Forward(n),
"down" => Command::Down(n),
"up" => Command::Up(n),
_ => panic!("unknown command `{}`", cmd),
})
}
}
fn main() {
let input: Vec<_> = INPUT
.lines()
.map(|s| s.parse::<Command>().unwrap())
.collect();
// part 1
let mut xpos = 0;
let mut depth = 0;
for cmd in input.iter() {
match cmd {
Command::Forward(n) => xpos += n,
Command::Down(n) => depth += n,
Command::Up(n) => depth -= n,
}
}
println!("part 1: {}", xpos * depth);
// part 2
let mut xpos = 0;
let mut depth = 0;
let mut aim = 0;
for cmd in input.iter() {
match cmd {
Command::Forward(n) => {
xpos += n;
depth += aim * n;
}
Command::Down(n) => aim += n,
Command::Up(n) => aim -= n,
}
}
println!("part 2: {}", xpos * depth);
}