2022-12-03 13:54:02 +00:00
|
|
|
#![allow(dead_code)]
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io;
|
|
|
|
use std::io::BufRead;
|
2022-12-03 14:04:06 +00:00
|
|
|
use std::iter::FromIterator;
|
2022-12-03 13:54:02 +00:00
|
|
|
use std::path::Path;
|
|
|
|
use std::time::Instant;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let now = Instant::now();
|
|
|
|
part_one();
|
|
|
|
let part_one_duration = now.elapsed();
|
|
|
|
part_two();
|
|
|
|
let part_two_duration = now.elapsed();
|
|
|
|
println!(
|
|
|
|
"p1: {}ms, p2: {}ms",
|
|
|
|
part_one_duration.as_millis(),
|
|
|
|
(part_two_duration - part_one_duration).as_millis()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
|
|
|
where
|
|
|
|
P: AsRef<Path>,
|
|
|
|
{
|
|
|
|
// note that this discards a final newline
|
|
|
|
let file = File::open(filename)?;
|
|
|
|
Ok(io::BufReader::new(file).lines())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn part_one() {
|
|
|
|
if let Ok(lines) = read_lines("./inputs/input") {
|
|
|
|
let mut priorities = 0;
|
|
|
|
for line in lines {
|
|
|
|
let mut knapsack = HashMap::<char, bool>::new();
|
|
|
|
if let Ok(contents) = line {
|
|
|
|
let pocket_one = &contents[0..contents.len() / 2];
|
|
|
|
let pocket_two = &contents[contents.len() / 2..];
|
|
|
|
for item in pocket_one.chars() {
|
|
|
|
knapsack.insert(item, true);
|
|
|
|
}
|
|
|
|
for item in pocket_two.chars() {
|
|
|
|
if knapsack.contains_key(&item) {
|
2022-12-03 14:36:44 +00:00
|
|
|
priorities += (item as u32 - 38) % 58;
|
2022-12-03 13:54:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println!("{}", priorities)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn part_two() {
|
|
|
|
if let Ok(lines) = read_lines("./inputs/input") {
|
|
|
|
let mut priorities = 0;
|
|
|
|
let mut group: Vec<HashSet<char>> = Vec::new();
|
|
|
|
for line in lines {
|
|
|
|
if let Ok(contents) = line {
|
|
|
|
// build the group
|
2022-12-03 14:04:06 +00:00
|
|
|
let knapsack = HashSet::from_iter(contents.chars());
|
2022-12-03 13:54:02 +00:00
|
|
|
group.push(knapsack);
|
|
|
|
if group.len() != 3 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if let (Some(one), Some(two), Some(three)) =
|
|
|
|
(group.get(0), group.get(1), group.get(2))
|
|
|
|
{
|
2022-12-03 14:04:06 +00:00
|
|
|
let intermediate_one: HashSet<&char> =
|
|
|
|
HashSet::from_iter(two.intersection(three));
|
|
|
|
let intermediate_two: HashSet<&char> =
|
|
|
|
HashSet::from_iter(one.intersection(three));
|
2022-12-03 13:54:02 +00:00
|
|
|
let mut result = intermediate_one.intersection(&intermediate_two);
|
|
|
|
if let Some(item) = result.next() {
|
2022-12-03 14:36:44 +00:00
|
|
|
// double pointer!
|
|
|
|
priorities += (**item as u32 - 38) % 58;
|
2022-12-03 13:54:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
group.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println!("{}", priorities)
|
|
|
|
}
|
|
|
|
}
|