/// --- Part Two --- /// /// As you finish identifying the misplaced items, the Elves come to you with another issue. /// /// For safety, the Elves are divided into groups of three. Every Elf carries a badge that /// identifies their group. For efficiency, within each group of three Elves, the badge is the /// only item type carried by all three Elves. That is, if a group's badge is item type B, then /// all three Elves will have item type B somewhere in their rucksack, and at most two of the Elves /// will be carrying any other item type. /// /// The problem is that someone forgot to put this year's updated authenticity sticker on the /// badges. All of the badges need to be pulled out of the rucksacks so the new authenticity /// stickers can be attached. /// /// Additionally, nobody wrote down which item type corresponds to each group's badges. The only /// way to tell which item type is the right one is by finding the one item type that is common /// between all three Elves in each group. /// /// Every set of three lines in your list corresponds to a single group, but each group can have a /// different badge item type. So, in the above example, the first group's rucksacks are the first /// three lines: /// /// ``` /// vJrwpWtwJgWrhcsFMMfFFhFp /// jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL /// PmmdzqPrVvPwwTWBwg /// ``` /// /// And the second group's rucksacks are the next three lines: /// /// ``` /// wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn /// ttgJtRGJQctTZtZT /// CrZsJsPPZsGzwwsLwLmpwMDw /// ``` /// /// In the first group, the only item type that appears in all three rucksacks is lowercase r; this /// must be their badges. In the second group, their badge item type must be Z. /// /// Priorities for these items must still be found to organize the sticker attachment efforts: /// here, they are 18 (r) for the first group and 52 (Z) for the second group. The sum of these is /// 70. /// /// Find the item type that corresponds to the badges of each three-Elf group. What is the sum of /// the priorities of those item types? use clap::Parser; use itertools::Itertools; use std::fs::File; use std::io::prelude::*; use std::io::BufReader; use std::path::PathBuf; const FILEPATH: &'static str = "examples/input.txt"; #[derive(Parser, Debug)] #[clap(author, version, about, long_about = None)] struct Cli { #[clap(short, long, default_value = FILEPATH)] file: PathBuf, } fn main() { let args = Cli::parse(); let file = File::open(&args.file).unwrap(); let reader = BufReader::new(file); let res = reader .lines() .map(|l| l.unwrap()) .chunks(3) .into_iter() .map(|iter| { let (top, middle, bottom) = iter.collect_tuple::<(String, String, String)>().unwrap(); top.chars() .filter(|t| middle.contains(*t) && bottom.contains(*t)) .map(|l| { let b = l as u8; if b > b'a' { (b - b'a') as u16 + 1 } else { (b - b'A') as u16 + 27 } }) .next() .unwrap() }) .sum::(); println!( "The sum of the priorities of the item that appear in both compartments of each rucksack is {}", res ); }