solution: day6

This commit is contained in:
2023-02-01 22:09:09 +01:00
parent 000000600a
commit 0000007042
2 changed files with 65 additions and 0 deletions

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

@@ -0,0 +1,49 @@
use hashbrown::HashMap;
use itertools::Itertools;
fn solve(input: &str, min: bool) -> String {
let mut data: HashMap<_, HashMap<_, _>> = HashMap::new();
for line in input.lines() {
for (idx, x) in line.chars().enumerate() {
*data.entry(idx).or_default().entry(x).or_insert(0) += 1;
}
}
if min {
data.iter()
.sorted_by_key(|(k, _)| *k)
.map(|(_, v)| *v.iter().min_by_key(|(_, v)| *v).unwrap().0)
.collect::<String>()
} else {
data.iter()
.sorted_by_key(|(k, _)| *k)
.map(|(_, v)| *v.iter().max_by_key(|(_, v)| *v).unwrap().0)
.collect::<String>()
}
}
pub fn part_one(input: &str) -> Option<String> {
Some(solve(input, false))
}
pub fn part_two(input: &str) -> Option<String> {
Some(solve(input, true))
}
fn main() {
let input = &aoc::read_file("inputs", 6);
aoc::solve!(1, part_one, input);
aoc::solve!(2, part_two, input);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let input = aoc::read_file("examples", 6);
assert_eq!(part_one(&input), Some("easter".to_string()));
}
#[test]
fn test_part_two() {
let input = aoc::read_file("examples", 6);
assert_eq!(part_two(&input), Some("advent".to_string()));
}
}

16
src/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