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
61
62
63
64
65
66
67
|
use std::str::FromStr;
use std::error::Error;
use std::fs;
use std::path::Path;
#[derive(PartialEq, Debug)]
enum Command {
Forward(i32),
Down(i32),
Up(i32)
}
#[derive(Debug)]
struct ParseCommandError;
impl FromStr for Command {
type Err = ParseCommandError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let i = s.find(' ').ok_or(ParseCommandError)?;
let num = s[i + 1..].parse::<i32>().map_err(|_| ParseCommandError)?;
match &s[..i] {
"forward" => Ok(Command::Forward(num)),
"down" => Ok(Command::Down(num)),
"up" => Ok(Command::Up(num)),
_ => Err(ParseCommandError)
}
}
}
#[cfg(part1)]
pub fn solve(path: &Path) -> Result<(), Box<dyn Error>> {
let data = fs::read_to_string(path)?;
let mut hor = 0;
let mut depth = 0;
for command in data.lines().filter_map(|x| x.parse::<Command>().ok()) {
match command {
Command::Forward(v) => hor += v,
Command::Down(v) => depth += v,
Command::Up(v) => depth -= v
}
}
println!("{}", hor * depth);
Ok(())
}
#[cfg(part2)]
pub fn solve(path: &Path) -> Result<(), Box<dyn Error>> {
let data = fs::read_to_string(path)?;
let mut aim = 0;
let mut hor = 0;
let mut depth = 0;
for command in data.lines().filter_map(|x| x.parse::<Command>().ok()) {
match command {
Command::Forward(v) => {
hor += v;
depth += aim * v;
},
Command::Down(v) => aim += v,
Command::Up(v) => aim -= v
}
}
println!("{}", hor * depth);
Ok(())
}
|