-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Connor Glosser
committed
Dec 2, 2024
1 parent
a4e36a4
commit 8d3ef75
Showing
4 changed files
with
66 additions
and
2 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
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,51 @@ | ||
use crate::utils::solution::Solution; | ||
use std::collections::{BinaryHeap, HashMap}; | ||
|
||
/// Puzzle: https://adventofcode.com/2024/day/1 | ||
pub struct Day01 {} | ||
|
||
impl Solution for Day01 { | ||
fn part_one(&self, input: &str) -> Option<String> { | ||
let (left, right) = sorted_lists(input); | ||
Some( | ||
left.iter() | ||
.zip(right.iter()) | ||
.map(|(a, b)| (b - a).abs()) | ||
.sum::<i32>() | ||
.to_string(), | ||
) | ||
} | ||
|
||
fn part_two(&self, input: &str) -> Option<String> { | ||
let (left, right) = sorted_lists(input); | ||
|
||
let counts = right | ||
.iter() | ||
.fold(HashMap::<i32, i32>::new(), |mut counts, &value| { | ||
*counts.entry(value).or_insert(0) += 1; | ||
counts | ||
}); | ||
|
||
Some( | ||
left.iter() | ||
.map(|value| value * counts.get(value).unwrap_or(&0)) | ||
.sum::<i32>() | ||
.to_string(), | ||
) | ||
} | ||
} | ||
|
||
fn sorted_lists(input: &str) -> (Vec<i32>, Vec<i32>) { | ||
let (left, right) = input.lines().fold( | ||
(BinaryHeap::<i32>::new(), BinaryHeap::<i32>::new()), | ||
|(mut left, mut right), line| { | ||
let (a, b) = line.split_once(" ").expect("Unable to split line at \" \""); | ||
left.push(a.trim().parse::<i32>().expect("Bad integer")); | ||
right.push(b.trim().parse::<i32>().expect("Bad integer")); | ||
|
||
(left, right) | ||
}, | ||
); | ||
|
||
(left.into_sorted_vec(), right.into_sorted_vec()) | ||
} |
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,11 @@ | ||
use super::DayAssociations; | ||
|
||
pub mod day_01; | ||
|
||
pub fn solutions() -> DayAssociations { | ||
let associations: Vec<(i32, super::BoxedSolution)> = vec![ | ||
(1, Box::new(day_01::Day01 {})), | ||
]; | ||
|
||
associations.into_iter().collect() | ||
} |
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