Day 3 solution, plus some mucking about with the template code.
This commit is contained in:
parent
355f5f6628
commit
0e22e82652
5 changed files with 366 additions and 7 deletions
53
2022/src/day03.rs
Normal file
53
2022/src/day03.rs
Normal file
|
@ -0,0 +1,53 @@
|
|||
use std::io;
|
||||
use std::vec::Vec;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use crate::utils;
|
||||
|
||||
pub fn execute() -> Result<(), io::Error> {
|
||||
let mut score = 0;
|
||||
let mut sacks: Vec<HashSet<u8>> = Vec::new();
|
||||
|
||||
for line in utils::read_lines("input/day03.txt")? {
|
||||
let text = line?;
|
||||
let (text1, text2) = text.split_at(text.len() / 2);
|
||||
let mut pouch1 = HashSet::new();
|
||||
let mut pouch2 = HashSet::new();
|
||||
for i in 0..text1.len() {
|
||||
pouch1.insert(text1.as_bytes()[i]);
|
||||
pouch2.insert(text2.as_bytes()[i]);
|
||||
}
|
||||
|
||||
let intersect: HashSet<_> = pouch1.intersection(&pouch2).cloned().collect();
|
||||
score += get_score(&intersect);
|
||||
|
||||
sacks.push(pouch1.union(&pouch2).cloned().collect());
|
||||
}
|
||||
|
||||
let mut score2 = 0;
|
||||
for group in sacks.chunks(3) {
|
||||
let mut intersect: HashSet<u8> = group[0].intersection(&group[1]).cloned().collect();
|
||||
intersect = intersect.intersection(&group[2]).cloned().collect();
|
||||
score2 += get_score(&intersect);
|
||||
}
|
||||
|
||||
utils::print_step(1, score as u64);
|
||||
utils::print_step(2, score2 as u64);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_score(intersect: &HashSet<u8>) -> u32 {
|
||||
if intersect.len() > 1 {
|
||||
panic!("More than one overlapping item.");
|
||||
}
|
||||
|
||||
if let Some(item) = intersect.iter().next() {
|
||||
if *item > 0x60 {
|
||||
return (*item - 0x60) as u32;
|
||||
}
|
||||
|
||||
return ((*item - 0x40) + 26) as u32;
|
||||
}
|
||||
|
||||
panic!("No items in intersection.");
|
||||
}
|
|
@ -1,11 +1,12 @@
|
|||
use std::env;
|
||||
use std::io;
|
||||
mod utils;
|
||||
|
||||
const LATEST: u8 = 2;
|
||||
const LATEST: u8 = 3;
|
||||
|
||||
mod day01;
|
||||
mod day02;
|
||||
// mod day03;
|
||||
mod day03;
|
||||
// mod day04;
|
||||
// mod day05;
|
||||
// mod day06;
|
||||
|
@ -34,7 +35,7 @@ fn main() -> io::Result<()> {
|
|||
match day {
|
||||
1 => day01::execute(),
|
||||
2 => day02::execute(),
|
||||
// 3 => day03::execute(),
|
||||
3 => day03::execute(),
|
||||
// 4 => day04::execute(),
|
||||
// 5 => day05::execute(),
|
||||
// 6 => day06::execute(),
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
mod utils;
|
||||
use std::io;
|
||||
|
||||
use crate::utils;
|
||||
|
||||
pub fn execute() -> Result<(), io::Error> {
|
||||
for line in utils::read_lines("input/day03.txt")? {
|
||||
let text = line?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -2,11 +2,11 @@ use std::io;
|
|||
use std::io::prelude::*;
|
||||
use std::fs::File;
|
||||
|
||||
fn read_lines(filename: &str) -> io::Result<impl Iterator<Item = io::Result<String>>> {
|
||||
pub fn read_lines(filename: &str) -> io::Result<impl Iterator<Item = io::Result<String>>> {
|
||||
let file = File::open(filename)?;
|
||||
Ok(io::BufReader::new(file).lines())
|
||||
}
|
||||
|
||||
fn print_step(step: i8, value: u64) {
|
||||
println!("Step {step} solution: {solution}");
|
||||
pub fn print_step(step: i8, value: u64) {
|
||||
println!("Step {step} solution: {value}");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue