-
Notifications
You must be signed in to change notification settings - Fork 0
/
02.rs
58 lines (52 loc) · 1.21 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
#![feature(test)]
enum Command {
Forward(i32),
Down(i32),
Up(i32),
}
type Input = Vec<Command>;
fn setup(input: &str) -> Input {
input
.lines()
.map(|line| {
let mut x = line.split(' ');
let cmd = x.next().unwrap();
let n: i32 = x.next().unwrap().parse().unwrap();
match cmd {
"forward" => Command::Forward(n),
"up" => Command::Up(n),
"down" => Command::Down(n),
_ => panic!(),
}
})
.collect()
}
fn part1(input: &Input) -> String {
let mut d = 0;
let mut h = 0;
for cmd in input {
match cmd {
Command::Forward(n) => h += n,
Command::Down(n) => d += n,
Command::Up(n) => d -= n,
}
}
(d * h).to_string()
}
fn part2(input: &Input) -> String {
let mut d = 0;
let mut h = 0;
let mut a = 0;
for cmd in input {
match cmd {
Command::Forward(n) => {
h += n;
d += a * n;
}
Command::Down(n) => a += n,
Command::Up(n) => a -= n,
}
}
(d * h).to_string()
}
aoc::main!(2021, 2, ex: 1);