day 3 done; first time really fighting the ownership rules
This commit is contained in:
parent
4d4958aefc
commit
51525f1e93
98
03/main.rs
Normal file
98
03/main.rs
Normal file
@ -0,0 +1,98 @@
|
||||
#![allow(dead_code)]
|
||||
use std::collections::hash_map::RandomState;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::io::BufRead;
|
||||
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) {
|
||||
if item as u32 > 96 {
|
||||
priorities += item as u32 - 96
|
||||
} else {
|
||||
priorities += item as u32 - 38
|
||||
}
|
||||
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
|
||||
let mut knapsack = HashSet::new();
|
||||
for item in contents.chars() {
|
||||
knapsack.insert(item);
|
||||
}
|
||||
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))
|
||||
{
|
||||
let intermediate_one: _ = two
|
||||
.intersection(three)
|
||||
.collect::<HashSet<&char, RandomState>>();
|
||||
let intermediate_two: _ = one
|
||||
.intersection(three)
|
||||
.collect::<HashSet<&char, RandomState>>();
|
||||
let mut result = intermediate_one.intersection(&intermediate_two);
|
||||
if let Some(item) = result.next() {
|
||||
if **item as u32 > 96 {
|
||||
priorities += **item as u32 - 96
|
||||
} else {
|
||||
priorities += **item as u32 - 38
|
||||
}
|
||||
}
|
||||
}
|
||||
group.clear();
|
||||
}
|
||||
}
|
||||
println!("{}", priorities)
|
||||
}
|
||||
}
|
@ -30,7 +30,7 @@ where
|
||||
fn part_one() {
|
||||
if let Ok(lines) = read_lines("./inputs/input") {
|
||||
for line in lines {
|
||||
// do stuff
|
||||
// do stuff
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user