-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
60 lines (51 loc) · 1.46 KB
/
main.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
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
const CAP_BIT: u8 = 0b0010_0000; // 32
pub fn main() {
let data = include_str!("input.txt");
let reduced = reduced_polymer(data.trim().as_bytes().iter().copied());
println!("Part 1: {}", part_one(&reduced));
println!("Part 2: {}", part_two(&reduced));
}
const fn part_one(reduced: &[u8]) -> usize {
reduced.len()
}
fn part_two(reduced: &[u8]) -> usize {
(b'a'..=b'z')
.map(|removed| {
reduced_polymer(
reduced
.iter()
.copied()
.filter(|&unit| unit | CAP_BIT != removed),
)
.len()
})
.min()
.unwrap()
}
fn reduced_polymer(input: impl Iterator<Item = u8>) -> Vec<u8> {
input.fold(Vec::with_capacity(20_000), |mut polymer, unit| {
if polymer.last().is_some_and(|&l| l ^ unit == CAP_BIT) {
polymer.pop();
} else {
polymer.push(unit);
}
polymer
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
let reduced = reduced_polymer(data.trim().as_bytes().iter().copied());
assert_eq!(10, part_one(&reduced));
}
#[test]
fn two() {
let data = include_str!("test.txt");
let reduced = reduced_polymer(data.trim().as_bytes().iter().copied());
assert_eq!(4, part_two(&reduced));
}
}