Skip to content

Commit

Permalink
Solve 2024 day 01
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor Glosser committed Dec 2, 2024
1 parent a4e36a4 commit 8d3ef75
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/lib/solutions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod year_2020;
pub mod year_2021;
pub mod year_2022;
pub mod year_2023;
pub mod year_2024;

type BoxedSolution = Box<dyn crate::utils::solution::Solution>;
type DayAssociations = std::collections::HashMap<i32, BoxedSolution>;
Expand All @@ -12,6 +13,7 @@ pub fn all_solutions() -> YearAssociations {
let associations: Vec<(i32, DayAssociations)> = vec![
(2015, year_2015::solutions()),
(2023, year_2023::solutions()),
(2024, year_2024::solutions()),
];
associations.into_iter().collect()
}
51 changes: 51 additions & 0 deletions src/lib/solutions/year_2024/day_01.rs
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())
}
11 changes: 11 additions & 0 deletions src/lib/solutions/year_2024/mod.rs
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()
}
4 changes: 2 additions & 2 deletions src/lib/utils/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ impl CommandLineInterface {
.version("v0.1")
.arg(
Arg::with_name(YEARS)
.default_value("2015-2022")
.default_value("2024")
.help("Specify yearly solutions to execute")
.long("year")
.short("y")
.takes_value(true)
.use_delimiter(true)
.validator(validate_int_sequence)
.value_name("[2015-2022]"),
.value_name("[2015-2024]"),
)
.arg(
Arg::with_name(DAYS)
Expand Down

0 comments on commit 8d3ef75

Please sign in to comment.