year2017/
day10.rs

1use crate::knot_hash::{knot_hash_hex, knot_rounds};
2use utils::prelude::*;
3
4/// Implementing a custom hash function.
5#[derive(Clone, Debug)]
6pub struct Day10<'a> {
7    input: &'a str,
8}
9
10impl<'a> Day10<'a> {
11    pub fn new(input: &'a str, _: InputType) -> Result<Self, InputError> {
12        // Parts 1 and 2 expect different input
13        Ok(Self { input })
14    }
15
16    #[must_use]
17    pub fn part1(&self) -> u32 {
18        let lengths = parser::u8()
19            .with_suffix(b','.or(parser::eof()))
20            .parse_all(self.input)
21            .expect("input invalid for part 1");
22
23        let list = knot_rounds(lengths.iter().copied(), 1);
24
25        list[0] as u32 * list[1] as u32
26    }
27
28    #[must_use]
29    pub fn part2(&self) -> String {
30        let hex = knot_hash_hex(self.input.bytes());
31
32        String::from_utf8(hex.to_vec()).unwrap()
33    }
34}
35
36examples!(Day10<'_> -> (u32, &'static str) [
37    {input: "", part2: "a2582a3a0e66e6e86e3812dcb672a272"},
38    {input: "AoC 2017", part2: "33efeb34ea91902bb2f59c9920caa6cd"},
39    {input: "1,2,3", part2: "3efbe78a8d82f29979031a4aa0b16a9d"},
40    {input: "1,2,4", part2: "63960835bcdc130f0b66d7ff4f6a5a8e"},
41]);