-
Notifications
You must be signed in to change notification settings - Fork 4
/
day2.rs
62 lines (54 loc) · 1.25 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
use std::vec::Vec;
fn parse<'a, I, S>(lines: I) -> Vec<(u8, u8)>
where
I: IntoIterator<Item = &'a S>,
S: AsRef<str> + 'a,
{
lines
.into_iter()
.filter_map(|line| {
let mut iter = line.as_ref().split_ascii_whitespace();
Some((
iter.next()?.as_bytes().first()? - 64,
iter.next()?.as_bytes().first()? - 87,
))
})
.collect()
}
fn score(left: u8, right: u8) -> u8 {
(4 + right - left) % 3 * 3 + right
}
pub fn part1<'a, I, S>(lines: I) -> u32
where
I: IntoIterator<Item = &'a S>,
S: AsRef<str> + 'a,
{
parse(lines)
.into_iter()
.map(|(left, right)| score(left, right) as u32)
.sum()
}
pub fn part2<'a, I, S>(lines: I) -> u32
where
I: IntoIterator<Item = &'a S>,
S: AsRef<str> + 'a,
{
parse(lines)
.into_iter()
.map(|(left, right)| score(left, 1 + (left + right) % 3) as u32)
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
static EXAMPLE: &[&str] = &["A Y", "B X", "C Z"];
#[test]
fn part1_examples() {
assert_eq!(15, part1(EXAMPLE));
}
#[test]
fn part2_examples() {
assert_eq!(12, part2(EXAMPLE));
}
}