use crate::utils; pub fn execute_task01(content: &str) { let vector = utils::convert_to_string_slice(&content); let rucksack_compartments: Vec<(Vec, Vec)> = vector.iter().map(|line| split_rucksack_in_two_compartments(line)).collect(); let duplicates: Vec = rucksack_compartments.iter() .flat_map(|(vec1, vec2)| vec1.iter() .copied() .flat_map(|entry| vec2.iter().copied().map(move |b| (entry, b))) .filter(|(a, b)| a == b) .map(|(a, _)| a)) .collect(); let sum: i32 = duplicates.iter().copied().map(|c| convert_char_to_prio(&c)).sum(); println!("Day03 - Task01 - Sum of Priorities: {}", sum); } pub fn execute_task02(content: &str) { let vector = utils::convert_to_string_slice(&content); let compartments: Vec> = vector.iter().map(|line| split_rucksack_in_items(line)).collect(); let mut duplicates: Vec = vec![]; for i in 0..(compartments.len()/3) { let first_index = i * 3 + 0; let second_index = i * 3 + 1; let third_index = i * 3 + 2; let first_vec = compartments.get(first_index).expect("ERR"); let second_vec = compartments.get(second_index).expect("ERR"); let third_vec = compartments.get(third_index).expect("ERR"); let mut dups: Vec = 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) } let sum: i32 = duplicates.iter().copied().map(|c| convert_char_to_prio(&c)).sum(); println!("Day03 - Task02 - Sum of Groups of Priorities: {}", sum); } fn split_rucksack_in_two_compartments(rucksack_content: &str) -> (Vec, Vec) { let first_compartment: &str = &rucksack_content[..rucksack_content.len()/2]; let second_compartment: &str = &rucksack_content[rucksack_content.len()/2..]; let mut first_compartment_vec: Vec = first_compartment.chars().map(|c| c).collect(); let mut second_compartment_vec: Vec = second_compartment.chars().map(|c| c).collect(); 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 { let mut vec: Vec = 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"); }