-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay02.rs
45 lines (39 loc) · 1.03 KB
/
Day02.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 std::env;
use std::io;
fn main() {
let stmts = io::stdin()
.lines()
.map(|s| {
let s = s.ok()?;
let mut w = s.split(" ");
let dir = w.next()?.to_owned();
let n = w.next()?.parse::<u32>().ok()?;
Some((dir, n))
})
.map(Option::unwrap);
let (mut x, mut y, mut aim) = (0, 0, 0);
if env::args().nth(1).unwrap() == "1" {
for inst in stmts {
match (inst.0.as_str(), inst.1) {
("forward", n) => x += n,
("down", n) => y += n,
("up", n) => y -= n,
_ => (),
}
}
} else {
// Part two
for inst in stmts {
match (inst.0.as_str(), inst.1) {
("down", n) => aim += n,
("up", n) => aim -= n,
("forward", n) => {
x += n;
y += aim * n;
}
_ => (),
}
}
}
println!("{}", x * y);
}