AoC 2024 - Day 1

This commit is contained in:
LunarAkai 2024-12-01 13:21:22 +01:00
commit 6079ff8c0d
6 changed files with 136 additions and 0 deletions

14
.gitignore vendored Normal file
View file

@ -0,0 +1,14 @@
# Rust
debug/
target/
Cargo.lock
*.pdb
idea/
# Puzzle Inputs
*.txt
# Added by cargo
/target

13
Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "advent_of_code_2024"
version = "0.1.0"
edition = "2021"
[[bin]]
name = "advent_of_code_2024"
path = "src/main.rs"
[lib]
name = "advent_of_code_2024"
path = "src/lib.rs"
[dependencies]

66
src/day_1/mod.rs Normal file
View file

@ -0,0 +1,66 @@
// Day 1: Historian Hysteria
/*
--- PART 1 ---
pair up smallest number in left list with smallest number in the right list
-> distances between those two numbers
-> add up all the distances
--- PART 2 ---
how often appears a number from the left list in the right list
-> calculate similarity score by adding up each number in the left list after
multiplying it by the number of times that number appears in the right list
*/
use std::collections::HashMap;
use crate::read_lines;
pub fn historian_hysteria() {
let result_p1 = historian_hysteria_part_one("src/day_1/input.txt");
let result_p2 = historian_hysteria_part_two("src/day_1/input.txt");
println!("PART 1 - total diff: {:?}", result_p1);
println!("PART 2 - similarity score: {:?}", result_p2);
}
pub fn historian_hysteria_part_one(path: &str) -> u32 {
let (left_numbers, right_numbers) = return_sets(path);
left_numbers
.iter()
.zip(right_numbers.iter()).map(|(left, right)| (left.abs_diff(*right)))
.sum()
}
pub fn historian_hysteria_part_two(path: &str) -> u32 {
let (left_numbers, right_numbers) = return_sets(path);
let mut counts = HashMap::with_capacity(left_numbers.len());
for number in right_numbers {
*counts.entry(number).or_insert(0) += 1;
}
left_numbers.iter().map(|i| i * counts.get(i).unwrap_or(&0)).sum()
}
pub fn return_sets(path: &str) -> (Vec<u32>, Vec<u32>) {
let mut left = Vec::new();
let mut right = Vec::new();
if let Ok(lines) = read_lines(path) {
for line in lines {
let new_line = line.unwrap();
let (a, b) = new_line.split_at(new_line.find(" ").unwrap());
left.push(a.parse::<u32>().unwrap());
right.push(b.trim().parse::<u32>().unwrap());
}
}
left.sort();
right.sort();
(left,right)
}

16
src/lib.rs Normal file
View file

@ -0,0 +1,16 @@
pub mod day_1;
use std::{fs::File, io::{self, BufRead}, path::Path};
pub fn advent_of_code_2024() {
day_1::historian_hysteria();
}
// from: rust-by-example
// The output is wrapped in a Result to allow matching on errors
// Returns an Iterator to the Reader of the lines of the file.
pub fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}

5
src/main.rs Normal file
View file

@ -0,0 +1,5 @@
use advent_of_code_2024::advent_of_code_2024;
pub fn main() {
advent_of_code_2024();
}

22
tests/day_one.rs Normal file
View file

@ -0,0 +1,22 @@
use advent_of_code_2024::day_1::*;
#[test]
fn test_d1_total_diff() {
assert_eq!(historian_hysteria_part_one("src/day_1/test_data.txt"), 11);
}
#[test]
fn test_d1_sets() {
let a: Vec<u32> = vec![1, 2, 3, 3, 3, 4];
assert_eq!(return_sets("src/day_1/test_data.txt").0, a);
let b: Vec<u32> = vec![3, 3, 3, 4, 5, 9];
assert_eq!(return_sets("src/day_1/test_data.txt").1, b);
}
#[test]
fn test_d1_total_sim_score() {
assert_eq!(historian_hysteria_part_two("src/day_1/test_data.txt"), 31);
}