-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.rs
124 lines (111 loc) · 2.86 KB
/
day2.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use super::util;
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::num::ParseIntError;
use std::str::FromStr;
#[derive(Clone, Debug)]
enum ParseMoveError {
Prefix(String),
Suffix(ParseIntError),
}
impl Display for ParseMoveError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
ParseMoveError::Prefix(string) => {
write!(f, "no parse: {:?}", string)
}
ParseMoveError::Suffix(error) => error.fmt(f),
}
}
}
impl Error for ParseMoveError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
ParseMoveError::Suffix(error) => Some(error),
_ => None,
}
}
}
impl From<ParseIntError> for ParseMoveError {
fn from(error: ParseIntError) -> Self {
ParseMoveError::Suffix(error)
}
}
enum Move {
Horizontal(i32),
Vertical(i32),
}
impl FromStr for Move {
type Err = ParseMoveError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Some(suffix) = s.strip_prefix("forward ") {
Ok(Move::Horizontal(suffix.parse()?))
} else if let Some(suffix) = s.strip_prefix("down ") {
Ok(Move::Vertical(suffix.parse()?))
} else if let Some(suffix) = s.strip_prefix("up ") {
Ok(Move::Vertical(-suffix.parse()?))
} else {
Err(ParseMoveError::Prefix(s.to_string()))
}
}
}
pub fn part1<'a, I, S>(lines: I) -> Result<i32, Box<dyn Error + Send + Sync>>
where
I: IntoIterator<Item = &'a S>,
S: AsRef<str> + 'a,
{
let (mut x, mut y) = (0, 0);
for m in util::parse_many::<'a, Move, _, _>(lines)?.iter() {
match m {
Move::Horizontal(d) => {
x += d;
}
Move::Vertical(d) => {
y += d;
}
}
}
Ok(x * y)
}
pub fn part2<'a, I, S>(lines: I) -> Result<i32, Box<dyn Error + Send + Sync>>
where
I: IntoIterator<Item = &'a S>,
S: AsRef<str> + 'a,
{
let (mut x, mut y, mut depth) = (0, 0, 0);
for m in util::parse_many::<'a, Move, _, _>(lines)?.iter() {
match m {
Move::Horizontal(d) => {
x += d;
y += d * depth
}
Move::Vertical(d) => {
depth += d;
}
}
}
Ok(x * y)
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
static EXAMPLE: &[&str] = &[
"forward 5",
"down 5",
"forward 8",
"up 3",
"down 8",
"forward 2",
];
#[test]
fn part1_examples() -> Result<(), Box<dyn Error + Send + Sync>> {
assert_eq!(150, part1(EXAMPLE)?);
Ok(())
}
#[test]
fn part2_examples() -> Result<(), Box<dyn Error + Send + Sync>> {
assert_eq!(900, part2(EXAMPLE)?);
Ok(())
}
}