summaryrefslogtreecommitdiffstats
path: root/day03a/src/main.rs
diff options
context:
space:
mode:
authorShivesh Mandalia <mail@shivesh.org>2022-12-20 21:38:16 +0000
committerShivesh Mandalia <mail@shivesh.org>2022-12-20 21:38:16 +0000
commita1c075cd023714d61a6c9d7bc30a1b711e648d7f (patch)
tree5a6fe056a9092ca402d8820a723868f39443d0fb /day03a/src/main.rs
parent277254232b852283cbc6bc6a0f31a588c458bf6a (diff)
downloadadvent_of_code_2022-a1c075cd023714d61a6c9d7bc30a1b711e648d7f.tar.gz
advent_of_code_2022-a1c075cd023714d61a6c9d7bc30a1b711e648d7f.zip
complete day 3
Diffstat (limited to 'day03a/src/main.rs')
-rw-r--r--day03a/src/main.rs105
1 files changed, 105 insertions, 0 deletions
diff --git a/day03a/src/main.rs b/day03a/src/main.rs
new file mode 100644
index 0000000..eafb25f
--- /dev/null
+++ b/day03a/src/main.rs
@@ -0,0 +1,105 @@
+/// --- Day 3: Rucksack Reorganization ---
+///
+/// One Elf has the important job of loading all of the rucksacks with supplies for the jungle
+/// journey. Unfortunately, that Elf didn't quite follow the packing instructions, and so a few
+/// items now need to be rearranged.
+///
+/// Each rucksack has two large compartments. All items of a given type are meant to go into
+/// exactly one of the two compartments. The Elf that did the packing failed to follow this rule
+/// for exactly one item type per rucksack.
+///
+/// The Elves have made a list of all of the items currently in each rucksack (your puzzle input),
+/// but they need your help finding the errors. Every item type is identified by a single lowercase
+/// or uppercase letter (that is, a and A refer to different types of items).
+///
+/// The list of items for each rucksack is given as characters all on a single line. A given
+/// rucksack always has the same number of items in each of its two compartments, so the first half
+/// of the characters represent items in the first compartment, while the second half of the
+/// characters represent items in the second compartment.
+///
+/// For example, suppose you have the following list of contents from six rucksacks:
+///
+/// ```
+/// vJrwpWtwJgWrhcsFMMfFFhFp
+/// jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
+/// PmmdzqPrVvPwwTWBwg
+/// wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
+/// ttgJtRGJQctTZtZT
+/// CrZsJsPPZsGzwwsLwLmpwMDw
+/// ```
+///
+/// The first rucksack contains the items vJrwpWtwJgWrhcsFMMfFFhFp, which means its first
+/// compartment contains the items vJrwpWtwJgWr, while the second compartment contains the
+/// items hcsFMMfFFhFp. The only item type that appears in both compartments is lowercase p.
+/// The second rucksack's compartments contain jqHRNqRjqzjGDLGL and rsFMfFZSrLrFZsSL. The only
+/// item type that appears in both compartments is uppercase L.
+/// The third rucksack's compartments contain PmmdzqPrV and vPwwTWBwg; the only common item
+/// type is uppercase P.
+/// The fourth rucksack's compartments only share item type v.
+/// The fifth rucksack's compartments only share item type t.
+/// The sixth rucksack's compartments only share item type s.
+///
+/// To help prioritize item rearrangement, every item type can be converted to a priority:
+///
+/// Lowercase item types a through z have priorities 1 through 26.
+/// Uppercase item types A through Z have priorities 27 through 52.
+///
+/// In the above example, the priority of the item type that appears in both compartments of each
+/// rucksack is 16 (p), 38 (L), 42 (P), 22 (v), 20 (t), and 19 (s); the sum of these is 157.
+///
+/// Find the item type that appears in both compartments of each rucksack. What is the sum of the
+/// priorities of those item types?
+use clap::Parser;
+
+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())
+ .inspect(|l| {
+ if l.len() % 2 != 0 {
+ panic!("odd number of items in rucksack")
+ }
+ })
+ .map(|l| {
+ let (l, r) = l.split_at(l.len() / 2);
+ (l.to_owned(), r.to_owned())
+ })
+ .map(|(lhs, rhs)| {
+ lhs.chars()
+ .filter(|l| rhs.contains(*l))
+ .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
+ );
+}