1use utils::prelude::*;
2
3#[derive(Clone, Debug)]
5pub struct Day08<'a> {
6 input: &'a str,
7}
8
9impl<'a> Day08<'a> {
10 pub fn new(input: &'a str, _: InputType) -> Result<Self, InputError> {
11 Ok(Self { input })
12 }
13
14 #[must_use]
15 pub fn part1(&self) -> u32 {
16 self.input
17 .bytes()
18 .fold((false, 0), |(escaped, diff), b| match (escaped, b) {
19 (false, b'"') | (true, b'\\' | b'"') => (false, diff + 1),
20 (false, b'\\') => (true, diff),
21 (true, b'x') => (false, diff + 3),
22 _ => (false, diff),
23 })
24 .1
25 }
26
27 #[must_use]
28 pub fn part2(&self) -> u32 {
29 self.input
30 .bytes()
31 .fold((false, 0), |(escaped, diff), b| match (escaped, b) {
32 (false, b'"') => (false, diff + 2),
33 (false, b'\\') => (true, diff + 1),
34 (true, b'\\' | b'"') => (false, diff + 1),
35 _ => (false, diff),
36 })
37 .1
38 }
39}
40
41examples!(Day08<'_> -> (u32, u32) [
42 {file: "day08_example0.txt", part1: 12, part2: 19},
43]);