refactor: day03

This commit is contained in:
Lennard Brinkhaus 2022-12-07 02:26:13 +01:00
parent 56bd3207db
commit 75acd3ea09
Signed by: lennard.brinkhaus
GPG Key ID: 286421EC53998B22

View File

@ -1,5 +1,7 @@
use std::collections::HashSet;
use crate::utils; use crate::utils;
pub fn execute_task01(content: &str) { pub fn execute_task01(content: &str) {
let sum = solve_task01(content); let sum = solve_task01(content);
@ -14,89 +16,40 @@ pub fn execute_task02(content: &str) {
fn solve_task01(content: &str) -> i32 { fn solve_task01(content: &str) -> i32 {
let vector = utils::convert_to_string_slice(&content); let vector = utils::convert_to_string_slice(&content);
let rucksack_compartments: Vec<(Vec<char>, Vec<char>)> = vector.iter().map(|line| split_rucksack_in_two_compartments(line)).collect();
let duplicates: Vec<char> = rucksack_compartments.iter() vector.into_iter()
.flat_map(|(vec1, vec2)| vec1.iter() .map(|x| [&x[..x.len()/2], &x[x.len()/2..]]
.copied() .into_iter()
.flat_map(|entry| vec2.iter().copied().map(move |b| (entry, b))) .map(|x| x.chars().collect::<HashSet<_>>())
.filter(|(a, b)| a == b) .reduce(|acc, x| &acc & &x).unwrap())
.map(|(a, _)| a)) .map(|x| *x.iter().next().unwrap())
.collect(); .map(|c| convert_char_to_prio(c) as i32)
.sum()
duplicates.iter().copied().map(|c| convert_char_to_prio(&c)).sum()
} }
fn solve_task02(content: &str) -> i32 { fn solve_task02(content: &str) -> i32 {
let vector = utils::convert_to_string_slice(&content); let vector = utils::convert_to_string_slice(&content);
let compartments: Vec<Vec<char>> = vector.iter().map(|line| split_rucksack_in_items(line)).collect();
let mut duplicates: Vec<char> = vec![];
for i in 0..(compartments.len()/3) { vector.chunks(3).into_iter()
let first_index = i * 3 + 0; .map(|content| [content[0], content[1], content[2]].into_iter()
let second_index = i * 3 + 1; .map(|x| x.chars().collect::<HashSet<_>>())
let third_index = i * 3 + 2; .reduce(|acc, x| &acc & &x).unwrap())
.map(|x| *x.iter().next().unwrap())
let first_vec = compartments.get(first_index).expect("ERR"); .map(|c| convert_char_to_prio(c) as i32)
let second_vec = compartments.get(second_index).expect("ERR"); .sum()
let third_vec = compartments.get(third_index).expect("ERR");
let mut dups: Vec<char> = first_vec.into_iter()
.copied()
.flat_map(|val1| second_vec.into_iter()
.copied()
.flat_map(move |val2| third_vec.into_iter().copied().map(move |val3| (val1, val2, val3))))
.filter(|(a, b, c)| a == b && b == c)
.map(|(a, _, _)| a)
.collect();
duplicates.append(&mut dups)
}
duplicates.iter().copied().map(|c| convert_char_to_prio(&c)).sum()
} }
fn split_rucksack_in_two_compartments(rucksack_content: &str) -> (Vec<char>, Vec<char>) { pub fn convert_char_to_prio(c: char) -> u8 {
let first_compartment: &str = &rucksack_content[..rucksack_content.len()/2]; let c = c as u8;
let second_compartment: &str = &rucksack_content[rucksack_content.len()/2..]; match c {
b'a'..=b'z' => c - b'a' + 1,
let mut first_compartment_vec: Vec<char> = first_compartment.chars().map(|c| c).collect(); b'A'..=b'Z' => c - b'A' + 27,
let mut second_compartment_vec: Vec<char> = second_compartment.chars().map(|c| c).collect(); _ => unreachable!(),
first_compartment_vec.sort();
first_compartment_vec.dedup();
second_compartment_vec.sort();
second_compartment_vec.dedup();
return ( first_compartment_vec, second_compartment_vec);
}
fn split_rucksack_in_items(rucksack_content: &str) -> Vec<char> {
let mut vec: Vec<char> = rucksack_content.chars().map(|c| c).collect();
vec.sort();
vec.dedup();
return vec;
}
pub fn convert_char_to_prio(c: &char) -> i32 {
let c_code = c.clone() as u8;
if 65 <= c_code && c_code < 91 {
return (c_code - 64 + 26) as i32;
} }
if 97 <= c_code && c_code < 123 {
return (c_code - 96) as i32;
}
panic!("never should called");
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
const EXAMPLE01: &'static str = include_str!("input/day03/example01"); const EXAMPLE01: &'static str = include_str!("input/day03/example01");