diff options
| author | Shivesh Mandalia <mail@shivesh.org> | 2022-12-20 21:38:16 +0000 |
|---|---|---|
| committer | Shivesh Mandalia <mail@shivesh.org> | 2022-12-20 21:38:16 +0000 |
| commit | a1c075cd023714d61a6c9d7bc30a1b711e648d7f (patch) | |
| tree | 5a6fe056a9092ca402d8820a723868f39443d0fb /day03b/src/main.rs | |
| parent | 277254232b852283cbc6bc6a0f31a588c458bf6a (diff) | |
| download | advent_of_code_2022-a1c075cd023714d61a6c9d7bc30a1b711e648d7f.tar.gz advent_of_code_2022-a1c075cd023714d61a6c9d7bc30a1b711e648d7f.zip | |
complete day 3
Diffstat (limited to 'day03b/src/main.rs')
| -rw-r--r-- | day03b/src/main.rs | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/day03b/src/main.rs b/day03b/src/main.rs new file mode 100644 index 0000000..f309d5e --- /dev/null +++ b/day03b/src/main.rs @@ -0,0 +1,94 @@ +/// --- 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::<u16>(); + + println!( + "The sum of the priorities of the item that appear in both compartments of each rucksack is {}", + res + ); +} |
