Add solution for 2024 day 01
This commit is contained in:
parent
f613854a56
commit
9ce742fd57
35
2024/day-01/common.rs
Normal file
35
2024/day-01/common.rs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct PuzzleInputs {
|
||||||
|
pub left: Vec<i32>,
|
||||||
|
pub right: Vec<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PuzzleInputs {
|
||||||
|
pub fn parse<P>(path: P) -> Self
|
||||||
|
where P: AsRef<Path>
|
||||||
|
{
|
||||||
|
let mut left = Vec::new();
|
||||||
|
let mut right = Vec::new();
|
||||||
|
|
||||||
|
let content = fs::read_to_string(path).unwrap();
|
||||||
|
|
||||||
|
for line in content.lines() {
|
||||||
|
let mut parts = line.split(' ');
|
||||||
|
|
||||||
|
// unwrap is very festive
|
||||||
|
let first = parts.next().unwrap();
|
||||||
|
let first = first.parse().unwrap();
|
||||||
|
let last = parts.last().unwrap();
|
||||||
|
let last = last.parse().unwrap();
|
||||||
|
|
||||||
|
left.push(first);
|
||||||
|
right.push(last);
|
||||||
|
}
|
||||||
|
|
||||||
|
Self { left, right }
|
||||||
|
}
|
||||||
|
}
|
22
2024/day-01/part_one.rs
Normal file
22
2024/day-01/part_one.rs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
mod common;
|
||||||
|
|
||||||
|
use common::PuzzleInputs;
|
||||||
|
|
||||||
|
|
||||||
|
const INPUT_FILE: &str = "puzzle.txt";
|
||||||
|
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut inputs = PuzzleInputs::parse(INPUT_FILE);
|
||||||
|
|
||||||
|
inputs.left.sort();
|
||||||
|
inputs.right.sort();
|
||||||
|
|
||||||
|
let mut sum = 0;
|
||||||
|
for (l, r) in inputs.left.into_iter().zip(inputs.right) {
|
||||||
|
let dist = l.abs_diff(r);
|
||||||
|
sum += dist;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{sum}");
|
||||||
|
}
|
30
2024/day-01/part_two.rs
Normal file
30
2024/day-01/part_two.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
mod common;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use common::PuzzleInputs;
|
||||||
|
|
||||||
|
type Hist = HashMap<i32, i32>;
|
||||||
|
|
||||||
|
|
||||||
|
const INPUT_FILE: &str = "puzzle.txt";
|
||||||
|
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let inputs = PuzzleInputs::parse(INPUT_FILE);
|
||||||
|
let mut hist = Hist::new();
|
||||||
|
|
||||||
|
for elem in inputs.right {
|
||||||
|
hist.entry(elem).and_modify(|e| *e += 1).or_insert(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut score = 0;
|
||||||
|
|
||||||
|
for elem in &inputs.left {
|
||||||
|
if let Some(x) = hist.get(elem) {
|
||||||
|
score += elem * x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{score:?}");
|
||||||
|
}
|
1000
2024/day-01/puzzle.txt
Normal file
1000
2024/day-01/puzzle.txt
Normal file
File diff suppressed because it is too large
Load Diff
6
2024/day-01/sample.txt
Normal file
6
2024/day-01/sample.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
3 4
|
||||||
|
4 3
|
||||||
|
2 5
|
||||||
|
1 3
|
||||||
|
3 9
|
||||||
|
3 3
|
Loading…
Reference in New Issue
Block a user