-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}, | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters