This commit is contained in:
RingOfStorms (Joshua Bell) 2025-04-28 22:44:25 -05:00
parent b036db60d2
commit 902917426f
9 changed files with 1234 additions and 1 deletions

7
2024/Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "aoc_2024"
version = "0.1.0"

6
2024/Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "aoc_2024"
version = "0.1.0"
edition = "2021"
[dependencies]

1008
2024/src/day1input.rs Normal file

File diff suppressed because it is too large Load diff

55
2024/src/main.rs Normal file
View file

@ -0,0 +1,55 @@
mod day1input;
use std::collections::HashMap;
use day1input::INPUT;
fn main() {
INPUT.lines().map(|line| {
let nums = line
.split(" ")
.map(|item| item.parse().expect("Input was not a valid number"))
.collect::<Vec<i32>>();
(nums[0], nums[1])
}).
// println!("Our Input\n\n{}", INPUT);
let mut left_column: Vec<i32> = vec![];
let mut right_column: Vec<i32> = vec![];
for line in INPUT.lines() {
let nums: Vec<i32> = line
.split(" ")
.map(|item| item.parse().expect("Input was not a valid number"))
.collect();
left_column.push(nums[0]);
right_column.push(nums[1]);
}
// Part 1
// left_column.sort();
// right_column.sort();
//
// let mut total_distance = 0;
// for i in 0..left_column.len() {
// let distance = (left_column[i] - right_column[i]).abs();
// total_distance += distance;
// }
// println!("part 1 answer: {}\n", total_distance);
// Part 2
// sodifjsdjif
let mut lookup: HashMap<i32, i32> = HashMap::new();
for n in right_column.iter() {
let count = lookup.entry(*n).or_insert(0);
*count += 1;
}
let mut sum = 0;
for n in left_column.iter() {
// how many times is `n` in the right column
let mut r = lookup.get(n).unwrap_or(&0); // O(1)
sum += n * r;
}
println!("part 2 answer: {}", sum);
}