-
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
1 parent
0e2ed1f
commit 9c4a30a
Showing
4 changed files
with
90 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,6 @@ | ||
3 4 | ||
4 3 | ||
2 5 | ||
1 3 | ||
3 9 | ||
3 3 |
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,7 @@ | ||
[ | ||
{ | ||
"day": 1, | ||
"part1": "1660292", | ||
"part2": "22776016" | ||
} | ||
] |
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,59 @@ | ||
use crate::aoc2024::Aoc2024; | ||
use crate::traits::days::Day1; | ||
use crate::traits::ParseInput; | ||
use crate::traits::Solution; | ||
|
||
impl ParseInput<Day1> for Aoc2024 { | ||
type Parsed = (Vec<u32>, Vec<u32>); | ||
|
||
fn parse_input(input: &str) -> Self::Parsed { | ||
let mut lefts = Vec::new(); | ||
let mut rights = Vec::new(); | ||
|
||
for line in input.lines() { | ||
let line = line.trim(); | ||
|
||
if line.is_empty() { | ||
continue; | ||
} | ||
|
||
let mut parts = line.split_ascii_whitespace(); | ||
let left = parts.next().unwrap().parse().unwrap(); | ||
let right = parts.next().unwrap().parse().unwrap(); | ||
|
||
lefts.push(left); | ||
rights.push(right); | ||
} | ||
|
||
(lefts, rights) | ||
} | ||
} | ||
|
||
impl Solution<Day1> for Aoc2024 { | ||
type Part1Output = u32; | ||
type Part2Output = u32; | ||
|
||
fn part1((left, right): &(Vec<u32>, Vec<u32>)) -> u32 { | ||
let mut left = left.clone(); | ||
left.sort(); | ||
|
||
let mut right = right.clone(); | ||
right.sort(); | ||
|
||
left.into_iter() | ||
.zip(right) | ||
.map(|(l, r)| l.abs_diff(r)) | ||
.sum() | ||
} | ||
|
||
fn part2((left, right): &(Vec<u32>, Vec<u32>)) -> u32 { | ||
let mut score = 0; | ||
|
||
for l in left { | ||
let count = right.iter().filter(|&r| r == l).count() as u32; | ||
score += l * count; | ||
} | ||
|
||
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,18 @@ | ||
use crate::helpers::{run, Results, TimingData}; | ||
use crate::traits::days::*; | ||
|
||
pub struct Aoc2024; | ||
|
||
pub mod day1; | ||
|
||
pub fn run_solution_for_day(day: u32, input: &str, results: Option<Results>) -> Option<TimingData> { | ||
let r = results | ||
.as_ref() | ||
.and_then(|r| r.results_for_day(day as usize)); | ||
|
||
let elapsed = match day { | ||
1 => run::<Aoc2024, Day1>(input, r), | ||
_ => return None, | ||
}; | ||
Some(elapsed) | ||
} |