Skip to content

Commit aed249f

Browse files
committed
day 1 part 1 and 2
1 parent 0e2ed1f commit aed249f

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

inputs/2024/day1_test.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
3 4
2+
4 3
3+
2 5
4+
1 3
5+
3 9
6+
3 3

results/2024.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"day": 1,
4+
"part1": "1660292",
5+
"part2": "22776016"
6+
}
7+
]

src/aoc2024/day1.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use crate::aoc2024::Aoc2024;
2+
use crate::traits::days::Day1;
3+
use crate::traits::ParseInput;
4+
use crate::traits::Solution;
5+
6+
impl ParseInput<Day1> for Aoc2024 {
7+
type Parsed = (Vec<u32>, Vec<u32>);
8+
9+
fn parse_input(input: &str) -> Self::Parsed {
10+
let mut lefts = Vec::new();
11+
let mut rights = Vec::new();
12+
13+
for line in input.lines() {
14+
let line = line.trim();
15+
16+
if line.is_empty() {
17+
continue
18+
}
19+
20+
let mut parts = line.split_ascii_whitespace();
21+
let left = parts.next().unwrap().parse().unwrap();
22+
let right = parts.next().unwrap().parse().unwrap();
23+
24+
lefts.push(left);
25+
rights.push(right);
26+
}
27+
28+
(lefts, rights)
29+
}
30+
}
31+
32+
impl Solution<Day1> for Aoc2024 {
33+
type Part1Output = u32;
34+
type Part2Output = u32;
35+
36+
fn part1((left, right): &(Vec<u32>, Vec<u32>)) -> u32 {
37+
let mut left = left.clone();
38+
left.sort();
39+
40+
let mut right = right.clone();
41+
right.sort();
42+
43+
left.into_iter().zip(right).map(|(l, r)| l.abs_diff(r)).sum()
44+
}
45+
46+
fn part2((left, right): &(Vec<u32>, Vec<u32>)) -> u32 {
47+
let mut score = 0;
48+
49+
for l in left {
50+
let count = right.iter().filter(|&r| r == l).count() as u32;
51+
score += l * count;
52+
}
53+
54+
score
55+
}
56+
}

src/aoc2024/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use crate::helpers::{run, Results, TimingData};
2+
use crate::traits::days::*;
3+
4+
pub struct Aoc2024;
5+
6+
pub mod day1;
7+
8+
pub fn run_solution_for_day(day: u32, input: &str, results: Option<Results>) -> Option<TimingData> {
9+
let r = results
10+
.as_ref()
11+
.and_then(|r| r.results_for_day(day as usize));
12+
13+
let elapsed = match day {
14+
1 => run::<Aoc2024, Day1>(input, r),
15+
_ => return None,
16+
};
17+
Some(elapsed)
18+
}

0 commit comments

Comments
 (0)