From 2dc8abfbc6b574c291b2ae8153f61920caf4b5c6 Mon Sep 17 00:00:00 2001 From: george Date: Sat, 3 Dec 2022 14:53:06 +0000 Subject: [PATCH] day 3 --- day02/src/main.rs | 2 +- day03/Cargo.lock | 16 +++++++++++++++ day03/Cargo.toml | 9 ++++++++ day03/src/main.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 day03/Cargo.lock create mode 100644 day03/Cargo.toml create mode 100644 day03/src/main.rs diff --git a/day02/src/main.rs b/day02/src/main.rs index 4785271..369279e 100644 --- a/day02/src/main.rs +++ b/day02/src/main.rs @@ -38,7 +38,7 @@ fn play_score(opponent: &str, player: &str) -> usize { player_win_score(player) + outcome as usize } -fn player_hand_for_outcome<'a>(outcome: &Outcome, opponent: &str) -> &'a str { +fn player_hand_for_outcome(outcome: &Outcome, opponent: &str) -> &'static str { match outcome { Outcome::Won => match opponent { "A" => "Y", diff --git a/day03/Cargo.lock b/day03/Cargo.lock new file mode 100644 index 0000000..89fb770 --- /dev/null +++ b/day03/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day03" +version = "0.1.0" +dependencies = [ + "rustc-hash", +] + +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" diff --git a/day03/Cargo.toml b/day03/Cargo.toml new file mode 100644 index 0000000..54c5b00 --- /dev/null +++ b/day03/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day03" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rustc-hash = "1.1.0" diff --git a/day03/src/main.rs b/day03/src/main.rs new file mode 100644 index 0000000..affdda9 --- /dev/null +++ b/day03/src/main.rs @@ -0,0 +1,52 @@ +use rustc_hash::FxHashSet; +use std::io::{self, BufRead}; + +const LOWERCASE_ASCII_OFFSET: usize = 64; +const UPPERCASE_ASCII_OFFSET: usize = 32; +const LOWERCASE_ALPHA_OFFSET: usize = 26; + +fn main() { + let lines = io::stdin().lock().lines().map(|l| l.unwrap()); + let mut first_half: FxHashSet; + let mut second_half: FxHashSet; + let mut p1_sum = 0usize; + let mut p2_sum = 0usize; + + let mut group_set: FxHashSet; + let mut groups: Vec = Vec::new(); + + for line in lines { + groups.push(line.clone()); + if groups.len() == 3 { + group_set = groups[0] + .chars() + .collect::>() + .intersection(&groups[1].chars().collect()) + .copied() + .collect::>() + .intersection(&groups[2].chars().collect()) + .copied() + .collect(); + p2_sum += type_to_priority(group_set.iter().next().unwrap()); + groups.clear(); + } + + let (first, second) = line.split_at(line.len() / 2); + first_half = first.chars().collect(); + second_half = second.chars().collect(); + let shared_type = first_half.intersection(&second_half).next().unwrap(); + p1_sum += type_to_priority(shared_type); + } + println!("P1: {}, P2: {}", p1_sum, p2_sum); +} + +fn type_to_priority(ptype: &char) -> usize { + let mut ascii_val = *ptype as usize; + ascii_val -= LOWERCASE_ASCII_OFFSET; + if ascii_val > UPPERCASE_ASCII_OFFSET { + ascii_val -= UPPERCASE_ASCII_OFFSET + } else { + ascii_val += LOWERCASE_ALPHA_OFFSET + } + ascii_val +}