feat: add day 6

This commit is contained in:
Xavier Morel
2024-11-19 17:09:12 +01:00
parent 98e6f49561
commit 3fd7e1a50e
3 changed files with 93 additions and 13 deletions

16
data/examples/06.txt Normal file
View File

@@ -0,0 +1,16 @@
eedadn
drvtee
eandsr
raavrd
atevrs
tsrnev
sdttsa
rasrtv
nssdts
ntnada
svetve
tesnvt
vntsnd
vrdear
dvrsen
enarar

View File

@@ -1,8 +1,7 @@
advent_of_code::solution!(5);
fn get_valid_digests(prefix: &str) -> impl Iterator<Item = String> + '_ {
(1..)
.flat_map(move |x| {
(1..).flat_map(move |x| {
let data = format!("{}{:?}", prefix, x);
let digest = format!("{:x}", md5::compute(data));
if digest.starts_with("00000") {

65
src/bin/06.rs Normal file
View File

@@ -0,0 +1,65 @@
advent_of_code::solution!(6);
use std::collections::HashMap;
fn compute_frequencies(input: &str) -> HashMap<usize, HashMap<char, usize>> {
let frequencies: HashMap<usize, HashMap<char, usize>> = HashMap::new();
input.split("\n").fold(frequencies, |mut frq, str| {
str.chars().enumerate().for_each(|(n, c)| {
let slot_frq = frq.entry(n).or_default();
*slot_frq.entry(c).or_default() += 1;
});
frq
})
}
pub fn part_one(input: &str) -> Option<String> {
let input = input.strip_suffix("\n").unwrap_or(input);
let mut res = compute_frequencies(input)
.into_iter()
.map(|(k, v)| {
(
k,
v.into_iter()
.max_by_key(|(_, cnt)| *cnt)
.map(|(chr, _)| chr),
)
})
.collect::<Vec<_>>();
res.sort_by_key(|(k, _)| *k);
Some(res.into_iter().map(|(_, v)| v.unwrap()).collect::<String>())
}
pub fn part_two(input: &str) -> Option<String> {
let input = input.strip_suffix("\n").unwrap_or(input);
let mut res = compute_frequencies(input)
.into_iter()
.map(|(k, v)| {
(
k,
v.into_iter()
.min_by_key(|(_, cnt)| *cnt)
.map(|(chr, _)| chr),
)
})
.collect::<Vec<_>>();
res.sort_by_key(|(k, _)| *k);
Some(res.into_iter().map(|(_, v)| v.unwrap()).collect::<String>())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some("easter".into()));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some("advent".into()));
}
}