summaryrefslogtreecommitdiffstats
path: root/day03b/src/main.rs
blob: f309d5e7a888b6b2ab26c969bc8c8a854bc96527 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
    );
}