day 3
parent
c4266bb16c
commit
2dc8abfbc6
@ -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"
|
@ -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"
|
@ -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<char>;
|
||||||
|
let mut second_half: FxHashSet<char>;
|
||||||
|
let mut p1_sum = 0usize;
|
||||||
|
let mut p2_sum = 0usize;
|
||||||
|
|
||||||
|
let mut group_set: FxHashSet<char>;
|
||||||
|
let mut groups: Vec<String> = Vec::new();
|
||||||
|
|
||||||
|
for line in lines {
|
||||||
|
groups.push(line.clone());
|
||||||
|
if groups.len() == 3 {
|
||||||
|
group_set = groups[0]
|
||||||
|
.chars()
|
||||||
|
.collect::<FxHashSet<char>>()
|
||||||
|
.intersection(&groups[1].chars().collect())
|
||||||
|
.copied()
|
||||||
|
.collect::<FxHashSet<char>>()
|
||||||
|
.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
|
||||||
|
}
|
Loading…
Reference in New Issue