commit 6079ff8c0d70238cfdcab73cce271f9c85e6d06c Author: LunarAkai Date: Sun Dec 1 13:21:22 2024 +0100 AoC 2024 - Day 1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b352a9c --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +# Rust +debug/ +target/ +Cargo.lock +*.pdb +idea/ + +# Puzzle Inputs +*.txt + + +# Added by cargo + +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..43f13ab --- /dev/null +++ b/Cargo.toml @@ -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] diff --git a/src/day_1/mod.rs b/src/day_1/mod.rs new file mode 100644 index 0000000..8a43b00 --- /dev/null +++ b/src/day_1/mod.rs @@ -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, Vec) { + 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::().unwrap()); + right.push(b.trim().parse::().unwrap()); + } + } + + left.sort(); + right.sort(); + + (left,right) +} + + diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..4a292d4 --- /dev/null +++ b/src/lib.rs @@ -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

(filename: P) -> io::Result>> + where P: AsRef, { + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..16d5a69 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,5 @@ +use advent_of_code_2024::advent_of_code_2024; + +pub fn main() { + advent_of_code_2024(); +} diff --git a/tests/day_one.rs b/tests/day_one.rs new file mode 100644 index 0000000..1b4310b --- /dev/null +++ b/tests/day_one.rs @@ -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 = vec![1, 2, 3, 3, 3, 4]; + + assert_eq!(return_sets("src/day_1/test_data.txt").0, a); + + let b: Vec = 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); +}