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
//! # Matchsticks
//!
//! While [regular expressions](https://en.wikipedia.org/wiki/Regular_expression) may feel like a
//! natural choice, it's much faster and easier to simply treat the input as a stream of raw
//! ASCII `u8` bytes including newlines.
//!
//! For part one we run a small state machine using [`fold`] to keep track of the current and
//! previous characters. If we encounter a hexadecimal escape then four characters become one so the
//! difference increases by three. The sequences `\\` and `\"` both increase the difference by one.
//! Each newline increases the difference by two since every line is enclosed with two quotes.
//!
//! Part two is even more straightforward with no need for statekeeping. Quotes and backslashes
//! need to be escaped so increase the difference by one. As before each newline increases by the
//! difference by two.
//!
//! [`fold`]: Iterator::fold
const NEWLINE: u8 = 10;
const QUOTE: u8 = 34;
const SLASH: u8 = 92;
const ESCAPE: u8 = 120;
pub fn parse(input: &str) -> &str {
input
}
pub fn part1(input: &str) -> u32 {
let (_, result) = input.bytes().fold((false, 0), |(flag, count), b| match (flag, b) {
(true, ESCAPE) => (false, count + 3),
(true, _) => (false, count + 1),
(false, SLASH) => (true, count),
(false, NEWLINE) => (false, count + 2),
_ => (false, count),
});
result
}
pub fn part2(input: &str) -> u32 {
input
.bytes()
.map(|b| match b {
QUOTE | SLASH => 1,
NEWLINE => 2,
_ => 0,
})
.sum()
}