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
//! # Stream Processing
//!
//! Computes both parts in a single pass.
type Input = (u32, u32);
pub fn parse(input: &str) -> Input {
let mut iter = input.bytes();
let mut groups = 0;
let mut depth = 1;
let mut characters = 0;
while let Some(b) = iter.next() {
match b {
b'<' => {
// Inner loop for garbage.
while let Some(b) = iter.next() {
match b {
b'!' => {
iter.next();
}
b'>' => break,
_ => characters += 1,
}
}
}
b'{' => {
groups += depth;
depth += 1;
}
b'}' => {
depth -= 1;
}
_ => (),
}
}
(groups, characters)
}
pub fn part1(input: &Input) -> u32 {
input.0
}
pub fn part2(input: &Input) -> u32 {
input.1
}