-
Notifications
You must be signed in to change notification settings - Fork 4
/
day3.rs
69 lines (62 loc) · 1.56 KB
/
day3.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
use super::util::iter_chunks;
fn items(input: &[u8]) -> u64 {
input.iter().fold(0, |acc, byte| {
acc | (1
<< (if byte & 32 != 0 {
byte & 31
} else {
(byte & 31) + 26
}))
})
}
pub fn part1<'a, I, S>(lines: I) -> u32
where
I: IntoIterator<Item = &'a S>,
S: AsRef<str> + 'a,
{
lines
.into_iter()
.map(|line| -> u32 {
let bytes = line.as_ref().as_bytes();
let (first, second) = bytes.split_at(bytes.len() / 2);
let bits = items(first) & items(second);
(0..u64::BITS).filter(|b| bits & (1 << b) != 0).sum()
})
.sum()
}
pub fn part2<'a, I, S>(lines: I) -> u32
where
I: IntoIterator<Item = &'a S>,
S: AsRef<str> + 'a,
{
iter_chunks(lines.into_iter())
.filter_map(|chunk: [_; 3]| {
chunk
.into_iter()
.map(|line| items(line.as_ref().as_bytes()))
.reduce(|x, y| x & y)
})
.map(u64::trailing_zeros)
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
static EXAMPLE: &[&str] = &[
"vJrwpWtwJgWrhcsFMMfFFhFp",
"jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL",
"PmmdzqPrVvPwwTWBwg",
"wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn",
"ttgJtRGJQctTZtZT",
"CrZsJsPPZsGzwwsLwLmpwMDw",
];
#[test]
fn part1_examples() {
assert_eq!(157, part1(EXAMPLE));
}
#[test]
fn part2_examples() {
assert_eq!(70, part2(EXAMPLE));
}
}