Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/run.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ jobs:
pip3 install -r scripts/requirements.txt
python3 ./scripts/fetch_inputs.py

- name: Run 2024 days
run: |
time ./target/release-lto/aoc --year 2024

- name: Run 2023 days
run: |
time ./target/release-lto/aoc --year 2023
Expand Down
6 changes: 6 additions & 0 deletions inputs/2024/day1_test.txt
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
7 changes: 7 additions & 0 deletions results/2024.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"day": 1,
"part1": "1660292",
"part2": "22776016"
}
]
2 changes: 1 addition & 1 deletion scripts/fetch_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
token = os.environ["AOC_SESSION"]
cookies = {"session": token}

for year in [2019, 2021, 2022, 2023]:
for year in [2019, 2021, 2022, 2023, 2024]:
for day in range(1, 25 + 1):
dt = datetime.datetime(year, 12, day)
path = f"inputs/{year}/day{day}.txt"
Expand Down
2 changes: 1 addition & 1 deletion scripts/new_day.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

parser = argparse.ArgumentParser(description='Create new day file.')
parser.add_argument('--year', type=int, help='year',
default=2023, choices=[2019, 2021, 2022, 2023])
default=2023, choices=[2019, 2021, 2022, 2023, 2024])
parser.add_argument('--day', type=int, help='day', required=True)

args = parser.parse_args()
Expand Down
59 changes: 59 additions & 0 deletions src/aoc2024/day1.rs
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
}
}
18 changes: 18 additions & 0 deletions src/aoc2024/mod.rs
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)
}
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod aoc2019;
mod aoc2021;
mod aoc2022;
mod aoc2023;
mod aoc2024;
mod grid;
mod helpers;
mod traits;
Expand All @@ -26,7 +27,7 @@ struct Options {
#[arg(long)]
test: bool,
/// Advent year
#[arg(long, default_value = "2023")]
#[arg(long, default_value = "2024")]
year: u32,
/// Advent day
#[arg(long)]
Expand All @@ -53,6 +54,7 @@ fn run_day(
2021 => aoc2021::run_solution_for_day,
2022 => aoc2022::run_solution_for_day,
2023 => aoc2023::run_solution_for_day,
2024 => aoc2024::run_solution_for_day,
_ => panic!("undefined year {year}"),
};

Expand Down