From 470d23af855d34e0f7981f32d48b4b043c1db454 Mon Sep 17 00:00:00 2001 From: sticks Date: Sun, 1 Dec 2024 13:51:42 -0600 Subject: [PATCH] aoc24: init and day one solves --- .gitignore | 1 + Cargo.lock | 7 +++++ Cargo.toml | 6 ++++ readme.md | 42 +++++++++++++++++++++++++++ src/days/day1/mod.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++ src/days/mod.rs | 1 + src/main.rs | 22 +++++++++++++++ 7 files changed, 146 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 readme.md create mode 100644 src/days/day1/mod.rs create mode 100644 src/days/mod.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..3484d6f --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aoc24" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..156b607 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "aoc24" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..2febf0a --- /dev/null +++ b/readme.md @@ -0,0 +1,42 @@ +# AOC 2024 +Welcome to my repository for the Advent of Code 2024. I will be solving the puzzles in Rust and posting my solutions here. +I also keep track of my time to solve each puzzle, so I can see how I improve over time. + +## Setup +To run the solutions, you need to have Rust installed. You can install it by following the instructions on the [official website](https://www.rust-lang.org/tools/install). + +## Usage +To run the solution for a specific day, use the following command: +```bash +cargo run + +# So a command to run the solution for day 1 would look like this: +cargo run 1 input/day1.txt # or whatever the path to the input file is +``` + +## Progress +- [x] Day 1 (solved in 10 minutes, 30 seconds) +- [ ] Day 2 +- [ ] Day 3 +- [ ] Day 4 +- [ ] Day 5 +- [ ] Day 6 +- [ ] Day 7 +- [ ] Day 8 +- [ ] Day 9 +- [ ] Day 10 +- [ ] Day 11 +- [ ] Day 12 +- [ ] Day 13 +- [ ] Day 14 +- [ ] Day 15 +- [ ] Day 16 +- [ ] Day 17 +- [ ] Day 18 +- [ ] Day 19 +- [ ] Day 20 +- [ ] Day 21 +- [ ] Day 22 +- [ ] Day 23 +- [ ] Day 24 +- [ ] Day 25 \ No newline at end of file diff --git a/src/days/day1/mod.rs b/src/days/day1/mod.rs new file mode 100644 index 0000000..c206718 --- /dev/null +++ b/src/days/day1/mod.rs @@ -0,0 +1,67 @@ +use std::collections::HashMap; +use std::fs; + +/// Read our input file and parse it into a vector of tuples of i32 +/// Panics if the file can't be read or if the input can't be parsed +macro_rules! read_input { + ($file:expr) => { + std::fs::read_to_string($file) + .unwrap() + .lines() + .map(|line| { + let mut parts = line.split_whitespace(); + ( + parts.next().unwrap().parse().unwrap(), + parts.next().unwrap().parse().unwrap(), + ) + }) + .collect::>() + }; +} + +pub fn run(input: &str) { + let input = read_input!(input); + + // Extract left and right lists from the input + let left_list: Vec = input.iter().map(|&(l, _)| l).collect(); + let right_list: Vec = input.iter().map(|&(_, r)| r).collect(); + + // Calculate the total distance + let total_distance = calculate_total_distance(left_list.clone(), right_list.clone()); + println!("The total distance is: {}", total_distance); + + // Calculate the similarity score + let similarity_score = calculate_similarity_score(left_list, right_list); + println!("The similarity score is: {}", similarity_score); +} + +fn calculate_total_distance(mut left: Vec, mut right: Vec) -> i32 { + // Sort both lists to pair smallest numbers with smallest numbers + left.sort(); + right.sort(); + + // Calculate the total distance + let mut total_distance = 0; + for i in 0..left.len() { + total_distance += (left[i] - right[i]).abs(); + } + + total_distance +} + +fn calculate_similarity_score(left: Vec, right: Vec) -> i32 { + // Count occurrences of each number in the right list + let mut right_count = HashMap::new(); + for &num in &right { + *right_count.entry(num).or_insert(0) += 1; + } + + // Calculate the similarity score + let mut similarity_score = 0; + for &num in &left { + let count = right_count.get(&num).unwrap_or(&0); + similarity_score += num * count; + } + + similarity_score +} diff --git a/src/days/mod.rs b/src/days/mod.rs new file mode 100644 index 0000000..8b2a328 --- /dev/null +++ b/src/days/mod.rs @@ -0,0 +1 @@ +pub mod day1; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..061b18a --- /dev/null +++ b/src/main.rs @@ -0,0 +1,22 @@ +// Use the days mod in main.rs: +mod days; +use crate::days::day1; + +fn main() { + // We need a day argument to know which day to run, and input for the input file + let args: Vec = std::env::args().collect(); + if args.len() != 3 { + eprintln!("Usage: {} ", args[0]); + std::process::exit(1); + } + + // Parse the day argument + let day = args[1].parse::().unwrap(); + let input = &args[2]; + + // Run the correct day + match day { + 1 => day1::run(input), + _ => eprintln!("Day {} not implemented yet", day), + } +}