Skip to content

Commit

Permalink
2017 day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
ictrobot committed Oct 25, 2024
1 parent 2b7dfaa commit 75696ad
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
59 changes: 59 additions & 0 deletions crates/year2017/src/day04.rs
Original file line number Diff line number Diff line change
@@ -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<Self, InputError> {
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},
]);
1 change: 1 addition & 0 deletions crates/year2017/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ utils::year!(2017 => year2017, ${
1 => day01::Day01<'_>,
2 => day02::Day02,
3 => day03::Day03,
4 => day04::Day04<'_>,
});

0 comments on commit 75696ad

Please sign in to comment.