Skip to content
Open
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
16 changes: 13 additions & 3 deletions benches/benchmark_aoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use advent_of_code_2024::{
implementation::{
eight::DayEightSolution, eleven::DayElevenSolution, five::DayFiveSolution,
four::DayFourSolution, nine::DayNineSolution, one::DayOneSolution, seven::DaySevenSolution,
six::DaySixSolution, ten::DayTenSolution, three::DayThreeSolution,
twelve::DayTwelveSolution, two::DayTwoSolution,
six::DaySixSolution, ten::DayTenSolution, thirteen::DayThirteenSolution,
three::DayThreeSolution, twelve::DayTwelveSolution, two::DayTwoSolution,
},
Solution,
};
Expand Down Expand Up @@ -118,6 +118,15 @@ fn benchmark_aoc_day_twelve(c: &mut Criterion) {
group.finish();
}

fn benchmark_aoc_day_thirteen(c: &mut Criterion) {
let day_thirteen = DayThirteenSolution::new();
let mut group = c.benchmark_group("AOC day 13");

group.bench_function("Solution one", |b| b.iter(|| day_thirteen.part_one()));
//group.bench_function("Solution two", |b| b.iter(|| day_thirteen.part_two()));
group.finish();
}

criterion_group!(
benches,
benchmark_aoc_day_one,
Expand All @@ -131,6 +140,7 @@ criterion_group!(
benchmark_aoc_day_nine,
benchmark_aoc_day_ten,
benchmark_aoc_day_eleven,
benchmark_aoc_day_twelve
benchmark_aoc_day_twelve,
benchmark_aoc_day_thirteen
);
criterion_main!(benches);
15 changes: 15 additions & 0 deletions data/test/test_13.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400

Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176

Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450

Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279
134 changes: 128 additions & 6 deletions src/implementation/thirteen.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,143 @@
use crate::Solution;

pub struct DayThirteenSolution {
data: Vec<String>,
button_configs: Vec<(Button, Button)>,
prizes: Vec<Prize>,
}

#[derive(Debug)]
struct Button {
x: u32,
y: u32,
}

struct Prize {
x: u32,
y: u32,
}

impl Solution for DayThirteenSolution {
const DAY: u8 = 13;

fn new() -> Self {
todo!("Implement new function for DayThirteenSolution")
let input = parse_input(Self::read_data_to_vec().unwrap());
DayThirteenSolution {
button_configs: input.0,
prizes: input.1,
}
}

fn part_one(&self) -> u32 {
let mut count: u32 = 0;
for (button_config, prize) in self.button_configs.iter().zip(self.prizes.iter()) {
if let Some(v) = find_minimum_tokens(
button_config.0.x,
button_config.0.y,
button_config.1.x,
button_config.1.y,
prize.x,
prize.y,
) {
count += v;
}
}
count
}

fn part_two(&self) -> u64 {
const CORRECTION: u64 = 10000000000000;
42
}
}

fn find_minimum_tokens(xa: u32, ya: u32, xb: u32, yb: u32, x: u32, y: u32) -> Option<u32> {
let mut min_tokens = None;

for a_press in 1..100 {
for b_press in 1..100 {
let current_x = xa * a_press + xb * b_press;
let current_y = ya * a_press + yb * b_press;

if current_x == x && current_y == y {
let tokens = 3 * a_press + b_press;
min_tokens = Some(match min_tokens {
Some(current_min) if tokens < current_min => tokens,
Some(current_min) => current_min,
None => tokens,
});
}
}
}

fn part_one(&self) -> ! {
todo!("Implement part_one function for DayThirteenSolution")
min_tokens
}

fn parse_input(input: Vec<String>) -> (Vec<(Button, Button)>, Vec<Prize>) {
let mut button_configs = Vec::new();
let mut prizes = Vec::new();
let filtered_input = input
.iter()
.filter(|x| x.len() > 0)
.collect::<Vec<&String>>();
for combo in filtered_input.chunks(3) {
button_configs.push((
Button {
x: combo[0].split("+").collect::<Vec<&str>>()[1]
.split(",")
.collect::<Vec<&str>>()[0]
.parse::<u32>()
.unwrap(),
y: combo[0].split("+").collect::<Vec<&str>>()[2]
.parse::<u32>()
.unwrap(),
},
Button {
x: combo[1].split("+").collect::<Vec<&str>>()[1]
.split(",")
.collect::<Vec<&str>>()[0]
.parse::<u32>()
.unwrap(),
y: combo[1].split("+").collect::<Vec<&str>>()[2]
.parse::<u32>()
.unwrap(),
},
));
prizes.push(Prize {
x: combo[2].split("=").collect::<Vec<&str>>()[1]
.split(",")
.collect::<Vec<&str>>()[0]
.parse::<u32>()
.unwrap(),
y: combo[2].split("=").collect::<Vec<&str>>()[2]
.parse::<u32>()
.unwrap(),
});
}
(button_configs, prizes)
}

fn part_two(&self) -> ! {
todo!("Implement part_two function for DayThirteenSolution")
#[cfg(test)]
mod tests {
use std::fs;

use super::*;
fn get_day_thirteen() -> DayThirteenSolution {
let data = parse_input(
fs::read_to_string("data/test/test_13.txt")
.unwrap()
.lines()
.map(|x| x.to_string())
.collect(),
);
DayThirteenSolution {
button_configs: data.0,
prizes: data.1,
}
}
#[test]
fn test_part_one() {
let day_thirteen = get_day_thirteen();
let output = day_thirteen.part_one();
assert_eq!(output, 480);
}
}
Loading