Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
aoc24: init and day one solves
- Loading branch information
0 parents
commit 470d23a
Showing
7 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "aoc24" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <day number> <input file> | ||
|
||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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::<Vec<(i32, i32)>>() | ||
}; | ||
} | ||
|
||
pub fn run(input: &str) { | ||
let input = read_input!(input); | ||
|
||
// Extract left and right lists from the input | ||
let left_list: Vec<i32> = input.iter().map(|&(l, _)| l).collect(); | ||
let right_list: Vec<i32> = 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<i32>, mut right: Vec<i32>) -> 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<i32>, right: Vec<i32>) -> 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod day1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> = std::env::args().collect(); | ||
if args.len() != 3 { | ||
eprintln!("Usage: {} <day> <input>", args[0]); | ||
std::process::exit(1); | ||
} | ||
|
||
// Parse the day argument | ||
let day = args[1].parse::<u32>().unwrap(); | ||
let input = &args[2]; | ||
|
||
// Run the correct day | ||
match day { | ||
1 => day1::run(input), | ||
_ => eprintln!("Day {} not implemented yet", day), | ||
} | ||
} |