day 3 done

This commit is contained in:
Thorn Avery 2022-12-06 10:38:24 +11:00
parent b17fc3d75c
commit 917b377f98
6 changed files with 501 additions and 0 deletions

View file

@ -1,2 +1,3 @@
pub mod problem_01;
pub mod problem_02;
pub mod problem_03;

View file

@ -1,5 +1,6 @@
use taoc2022::problem_01;
use taoc2022::problem_02;
use taoc2022::problem_03;
fn main() {
println!("== Problem 01 ==");
@ -8,4 +9,7 @@ fn main() {
println!("== Problem 02 ==");
println!("= a: {}", problem_02::solution_a());
println!("= b: {}", problem_02::solution_b());
println!("== Problem 03 ==");
println!("= a: {}", problem_03::solution_a());
println!("= b: {}", problem_03::solution_b());
}

View file

@ -0,0 +1,67 @@
--- 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?
Your puzzle answer was 8153.
--- 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?
Your puzzle answer was 2342.
Both parts of this puzzle are complete! They provide two gold stars: **

300
src/problem_03/input.txt Normal file
View file

@ -0,0 +1,300 @@
VdzVHmNpdVm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bTbRFRphnnpsWgmFRPR
vTLHHCQLHBBjJCSZrVCZtSfSfrft
gHfHffHLjwHrRjLrLRZVMnTdTBsNTBwTVBsBnN
DWPhqhhDhvSGvWPzSzMBQBQVMMBBmvssvQvQ
CDGbqCDbChSbWGrHcHRgbcVcfrLJ
frlTLmtllbbbdpJS
qFjhzjThjHTFGHTjqhhjMzBhVpVpdbBnSJQRpBnVVdbRRQJd
vjWPWjWPPPWgwmfCrNvTvZ

91
src/problem_03/mod.rs Normal file
View file

@ -0,0 +1,91 @@
use std::cmp::Ordering;
static INPUT: &str = include_str!("input.txt");
fn item_priority(c: char) -> u8 {
if !c.is_ascii_alphabetic() {
panic!("invalid item in rucksack: {}", c);
}
match c {
'a'..='z' => (c as u8) - b'a' + 1,
_ => (c as u8) - b'A' + 27,
}
}
fn split_input_line(l: String) -> (Vec<char>, Vec<char>) {
let n = l.len();
if n % 2 != 0 {
panic!("invalid line length");
}
let s = n / 2;
let mut c1 = l[0..s].chars().collect::<Vec<_>>();
c1.sort();
c1.dedup();
let mut c2 = l[s..n].chars().collect::<Vec<_>>();
c2.sort();
c2.dedup();
(c1, c2)
}
fn parse_input_line(l: String) -> Vec<char> {
let mut c1 = l.chars().collect::<Vec<_>>();
c1.sort();
c1.dedup();
c1
}
fn find_items(c1: Vec<char>, c2: Vec<char>) -> Vec<char> {
let mut results = vec![];
let mut i1 = c1.iter().peekable();
let mut i2 = c2.iter().peekable();
while let (Some(a), Some(b)) = (i1.peek(), i2.peek()) {
match a.cmp(b) {
Ordering::Equal => {
results.push(**a);
i1.next();
i2.next();
}
Ordering::Less => {
i1.next();
}
Ordering::Greater => {
i2.next();
}
}
}
results
}
pub fn solution_a() -> i32 {
let input = INPUT.lines().map(String::from).map(split_input_line);
input
.into_iter()
.map(|(a, b)| find_items(a, b))
.map(|x| match x.first() {
Some(y) => item_priority(*y) as i32,
_ => panic!("no matches found"),
})
.sum()
}
pub fn solution_b() -> i32 {
let mut total = 0;
let mut input = INPUT.lines().map(String::from).map(parse_input_line);
loop {
let l1 = input.next();
let l2 = input.next();
let l3 = input.next();
match (l1, l2, l3) {
(Some(s1), Some(s2), Some(s3)) => {
let intermediate_matches = find_items(s1, s2);
let final_matches = find_items(s3, intermediate_matches);
match final_matches.first() {
Some(x) => total += item_priority(*x) as i32,
_ => panic!("no matches found"),
}
}
_ => break,
}
}
total
}

38
src/problem_03/types.rs Normal file
View file

@ -0,0 +1,38 @@
#[derive(Eq, PartialEq, Clone, Copy)]
pub enum RpsHand {
Paper,
Scissors,
Rock,
}
impl RpsHand {
pub fn loses_to(&self) -> RpsHand {
match *self {
RpsHand::Paper => RpsHand::Scissors,
RpsHand::Scissors => RpsHand::Rock,
RpsHand::Rock => RpsHand::Paper,
}
}
pub fn wins_against(&self) -> RpsHand {
match *self {
RpsHand::Paper => RpsHand::Rock,
RpsHand::Scissors => RpsHand::Paper,
RpsHand::Rock => RpsHand::Scissors,
}
}
pub fn ties_with(&self) -> RpsHand {
*self
}
}
pub enum Goal {
Lose,
Draw,
Win,
}
#[derive(Eq, PartialEq)]
pub struct Game {
pub opponent: RpsHand,
pub mine: RpsHand,
}