From 75696adf3cc27c8d0edbc16273c5e13ccaa0ac28 Mon Sep 17 00:00:00 2001 From: ictrobot Date: Fri, 25 Oct 2024 19:40:14 +0100 Subject: [PATCH] 2017 day 4 --- crates/year2017/src/day04.rs | 59 ++++++++++++++++++++++++++++++++++++ crates/year2017/src/lib.rs | 1 + 2 files changed, 60 insertions(+) create mode 100644 crates/year2017/src/day04.rs diff --git a/crates/year2017/src/day04.rs b/crates/year2017/src/day04.rs new file mode 100644 index 0000000..c6109d9 --- /dev/null +++ b/crates/year2017/src/day04.rs @@ -0,0 +1,59 @@ +use std::collections::HashSet; +use utils::prelude::*; + +/// Checking passphrase validity. +#[derive(Clone, Debug)] +pub struct Day04<'a> { + input: &'a str, +} + +impl<'a> Day04<'a> { + pub fn new(input: &'a str, _: InputType) -> Result { + if let Some(w) = input.find(|c: char| !c.is_ascii_lowercase() && !c.is_ascii_whitespace()) { + Err(InputError::new(input, w, "expected lowercase letters")) + } else { + Ok(Self { input }) + } + } + + #[must_use] + pub fn part1(&self) -> usize { + let mut seen = HashSet::new(); + self.input + .lines() + .filter(|line| { + seen.clear(); + line.split_ascii_whitespace().all(|word| seen.insert(word)) + }) + .count() + } + + #[must_use] + pub fn part2(&self) -> usize { + let mut seen = HashSet::new(); + self.input + .lines() + .filter(|line| { + seen.clear(); + line.split_ascii_whitespace().all(|word| { + let mut letters = [0u8; 26]; + for b in word.bytes() { + letters[b as usize - b'a' as usize] += 1; + } + seen.insert(letters) + }) + }) + .count() + } +} + +examples!(Day04<'_> -> (usize, usize) [ + {input: "aa bb cc dd ee", part1: 1}, + {input: "aa bb cc dd aa", part1: 0}, + {input: "aa bb cc dd aaa", part1: 1}, + {input: "abcde fghij", part2: 1}, + {input: "abcde xyz ecdab", part2: 0}, + {input: "a ab abc abd abf abj", part2: 1}, + {input: "iiii oiii ooii oooi oooo", part2: 1}, + {input: "oiii ioii iioi iiio", part2: 0}, +]); diff --git a/crates/year2017/src/lib.rs b/crates/year2017/src/lib.rs index 5c0ce82..204c459 100644 --- a/crates/year2017/src/lib.rs +++ b/crates/year2017/src/lib.rs @@ -5,4 +5,5 @@ utils::year!(2017 => year2017, ${ 1 => day01::Day01<'_>, 2 => day02::Day02, 3 => day03::Day03, + 4 => day04::Day04<'_>, });