Skip to content

Commit

Permalink
day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Alin committed Dec 12, 2024
1 parent bcc78fb commit 2331d05
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 79 deletions.
1 change: 1 addition & 0 deletions assets/input_day_11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0 4 4979 24 4356119 914 85734 698829
60 changes: 32 additions & 28 deletions benchmarks/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,47 +105,51 @@ mod day_06_benches {
mod day_07_benches {
use super::*;

#[divan::bench]
fn part_one() {
day_07::part_one(divan::black_box(include_str!("../assets/input_day_07.txt")));
}

#[divan::bench]
fn part_one_rtl() {
day_07::part_one_rtl(divan::black_box(include_str!("../assets/input_day_07.txt")));
}
// #[divan::bench]
// fn part_one() {
// day_07::part_one(divan::black_box(include_str!("../assets/input_day_07.txt")));
// }

// #[divan::bench]
// fn part_one_rtl() {
// day_07::part_one_rtl(divan::black_box(include_str!("../assets/input_day_07.txt")));
// }
#[divan::bench]
fn part_one_filter() {
day_07::part_one_filter(divan::black_box(include_str!("../assets/input_day_07.txt")));
}

#[divan::bench(sample_count = 10)]
fn part_two() {
day_07::part_two(divan::black_box(include_str!("../assets/input_day_07.txt")));
}
// #[divan::bench(sample_count = 10)]
// fn part_two() {
// day_07::part_two(divan::black_box(include_str!("../assets/input_day_07.txt")));
// }

#[divan::bench(sample_count = 10)]
fn part_two_rtl() {
day_07::part_two_rtl(divan::black_box(include_str!("../assets/input_day_07.txt")));
}
// #[divan::bench(sample_count = 10)]
// fn part_two_rtl() {
// day_07::part_two_rtl(divan::black_box(include_str!("../assets/input_day_07.txt")));
// }

#[divan::bench]
fn part_two_filter() {
day_07::part_two_filter(divan::black_box(include_str!("../assets/input_day_07.txt")));
}
}

// mod day_10_benches {
// use super::*;
mod day_11_benches {
use super::*;

// #[divan::bench]
// fn part_one() {
// day_10::part_one(divan::black_box(include_str!("../assets/input_day_10.txt")));
// }
#[divan::bench]
fn part_one() {
day_11::part_one(divan::black_box(include_str!("../assets/input_day_11.txt")));
}

// // #[divan::bench]
// // fn part_one_rtl() {
// // day_10::part_one_rtl(divan::black_box(include_str!("../assets/input_day_10.txt")));
// // }
// }
#[divan::bench]
fn part_two() {
day_11::part_two(divan::black_box(include_str!("../assets/input_day_11.txt")));
}

// #[divan::bench]
// fn part_one_rtl() {
// day_10::part_one_rtl(divan::black_box(include_str!("../assets/input_day_10.txt")));
// }
}
41 changes: 20 additions & 21 deletions benchmarks/output.txt
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
benchmarks fastest │ slowest │ median │ mean │ samples │ iters
├─ day_01_benches │ │ │ │ │
│ ├─ part_one 52.2 µs │ 128.2 µs │ 52.64 µs │ 53.66 µs │ 100 │ 100
│ ├─ part_one_zip 49.87 µs │ 54.12 µs │ 50.43 µs │ 51.12 µs │ 100 │ 100
│ ├─ part_two 73.04 µs │ 87.45 µs │ 77.18 µs │ 77.13 µs │ 100 │ 100
│ ╰─ part_two_filter 77.66 µs │ 82.33 µs │ 79.41 µs │ 79.5 µs │ 100 │ 100
│ ├─ part_one 49.33 µs │ 149.4 µs │ 50.95 µs │ 51.71 µs │ 100 │ 100
│ ├─ part_one_zip 49.08 µs │ 51.7 µs │ 49.49 µs │ 49.95 µs │ 100 │ 100
│ ├─ part_two 72.66 µs │ 120.8 µs │ 76.6 µs │ 78.1 µs │ 100 │ 100
│ ╰─ part_two_filter 76.99 µs │ 82.41 µs │ 79.49 µs │ 79.37 µs │ 100 │ 100
├─ day_02_benches │ │ │ │ │
│ ├─ part_one 125.7 µs │ 156 µs │ 126.1 µs │ 126.9 µs │ 100 │ 100
│ ╰─ part_two 161.9 µs │ 339.5 µs │ 163.1 µs │ 165.9 µs │ 100 │ 100
│ ├─ part_one 129.3 µs │ 164.4 µs │ 129.8 µs │ 130.4 µs │ 100 │ 100
│ ╰─ part_two 166.3 µs │ 183.1 µs │ 166.8 µs │ 167.3 µs │ 100 │ 100
├─ day_03_benches │ │ │ │ │
│ ├─ part_one 231.3 µs │ 1.234 ms235.8 µs │ 260.2 µs │ 100 │ 100
│ ├─ part_two 288.1 µs │ 470.8 µs │ 306.8 µs │ 310.9 µs │ 100 │ 100
│ ╰─ part_two_split 197.5 µs │ 848.3 µs │ 206.3 µs │ 225.2 µs │ 100 │ 100
│ ├─ part_one 229.7 µs │ 717.9 µs239.4 µs │ 244.9 µs │ 100 │ 100
│ ├─ part_two 288.5 µs │ 468.6 µs │ 301.8 µs │ 307.3 µs │ 100 │ 100
│ ╰─ part_two_split 200.7 µs │ 261.8 µs │ 207.3 µs │ 210.1 µs │ 100 │ 100
├─ day_04_benches │ │ │ │ │
│ ├─ part_one 342.6 µs │ 470.7 µs │ 350.1 µs │ 363.9 µs │ 100 │ 100
│ ╰─ part_two 332.7 µs │ 440.9 µs │ 340.8 µs │ 349.2 µs │ 100 │ 100
│ ├─ part_one 347.7 µs │ 450.4 µs │ 358.1 µs │ 362.8 µs │ 100 │ 100
│ ╰─ part_two 345.6 µs │ 369.2 µs │ 347.2 µs │ 350.2 µs │ 100 │ 100
├─ day_05_benches │ │ │ │ │
│ ├─ part_one 214.1 µs │ 269.4 µs │ 224.8 µs │ 226.8 µs │ 100 │ 100
│ ╰─ part_two 325.7 µs │ 389.9 µs │ 345.4 µs │ 348.4 µs │ 100 │ 100
│ ├─ part_one 211.3 µs │ 244.5 µs │ 221 µs │ 221.4 µs │ 100 │ 100
│ ╰─ part_two 288.7 µs │ 360.1 µs │ 312.6 µs │ 314.5 µs │ 100 │ 100
├─ day_06_benches │ │ │ │ │
│ ╰─ part_one 52.49 µs │ 74.45 µs │ 53.35 µs │ 54.41 µs │ 100 │ 100
╰─ day_07_benches │ │ │ │ │
├─ part_one 163.4 ms │ 275.5 ms │ 167.5 ms │ 168.8 ms │ 100 │ 100
├─ part_one_filter 13.88 ms │ 15.08 ms │ 14.11 ms │ 14.19 ms │ 100 │ 100
├─ part_one_rtl 157.4 ms │ 167.3 ms │ 158.7 ms │ 159.8 ms │ 100 │ 100
├─ part_two 9.179 s │ 9.497 s │ 9.258 s │ 9.274 s │ 10 │ 10
├─ part_two_filter 2.53 s │ 2.82 s │ 2.561 s │ 2.575 s │ 100 │ 100
╰─ part_two_rtl 7.139 s │ 7.3 s │ 7.191 s │ 7.195 s │ 10 │ 10
│ ╰─ part_one 54.16 µs │ 73.29 µs │ 54.41 µs │ 55.03 µs │ 100 │ 100
├─ day_07_benches │ │ │ │ │
│ ├─ part_one_filter 14.04 ms │ 15.5 ms │ 14.6 ms │ 14.57 ms │ 100 │ 100
│ ╰─ part_two_filter 2.584 s │ 2.835 s │ 2.634 s │ 2.646 s │ 100 │ 100
╰─ day_11_benches │ │ │ │ │
├─ part_one 414.7 µs │ 513 µs │ 422.6 µs │ 428.3 µs │ 100 │ 100
╰─ part_two 14.34 ms │ 15.74 ms │ 15.1 ms │ 15.1 ms │ 100 │ 100

69 changes: 69 additions & 0 deletions src/day_11/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::collections::HashMap;

pub fn part_one(file: &str) -> i64 {
let mut frequencies = HashMap::new();
for stone in file.split_whitespace().map(|s| s.parse::<i64>().unwrap()) {
*frequencies.entry(stone).or_insert(0) += 1;
}

for _ in 0..25 {
let mut new_frequencies = HashMap::new();
for (stone, count) in frequencies.iter() {
match stone {
0 => {
*new_frequencies.entry(1).or_insert(0) += count;
}
_ if stone.to_string().len() % 2 == 0 => {
let stone_str = stone.to_string();
let mid = stone_str.len() / 2;
let (left, right) = stone_str.split_at(mid);
let left: i64 = left.parse().unwrap();
let right: i64 = right.parse().unwrap();
*new_frequencies.entry(left).or_insert(0) += count;
*new_frequencies.entry(right).or_insert(0) += count;
}
_ => {
let new_stone = stone * 2024;
*new_frequencies.entry(new_stone).or_insert(0) += count;
}
}
}
frequencies = new_frequencies;
}

frequencies.iter().map(|(_, count)| *count).sum::<usize>() as i64
}

pub fn part_two(file: &str) -> i64 {
let mut frequencies = HashMap::new();
for stone in file.split_whitespace().map(|s| s.parse::<i64>().unwrap()) {
*frequencies.entry(stone).or_insert(0) += 1;
}

for _ in 0..75 {
let mut new_frequencies = HashMap::new();
for (stone, count) in frequencies.iter() {
match stone {
0 => {
*new_frequencies.entry(1).or_insert(0) += count;
}
_ if stone.to_string().len() % 2 == 0 => {
let stone_str = stone.to_string();
let mid = stone_str.len() / 2;
let (left, right) = stone_str.split_at(mid);
let left: i64 = left.parse().unwrap();
let right: i64 = right.parse().unwrap();
*new_frequencies.entry(left).or_insert(0) += count;
*new_frequencies.entry(right).or_insert(0) += count;
}
_ => {
let new_stone = stone * 2024;
*new_frequencies.entry(new_stone).or_insert(0) += count;
}
}
}
frequencies = new_frequencies;
}

frequencies.iter().map(|(_, count)| *count).sum::<i64>()
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod day_05;
pub mod day_06;
pub mod day_07;
pub mod day_10;
pub mod day_11;
63 changes: 33 additions & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,47 @@
use aoc_2024::*;

fn main() {
let d1_input = include_str!("../assets/input_day_01.txt");
let d2_input = include_str!("../assets/input_day_02.txt");
let d3_input = include_str!("../assets/input_day_03.txt");
let d4_input = include_str!("../assets/input_day_04.txt");
let d5_input = include_str!("../assets/input_day_05.txt");
let d6_input = include_str!("../assets/input_day_06.txt");
let d7_input = include_str!("../assets/input_day_07.txt");
// let d1_input = include_str!("../assets/input_day_01.txt");
// let d2_input = include_str!("../assets/input_day_02.txt");
// let d3_input = include_str!("../assets/input_day_03.txt");
// let d4_input = include_str!("../assets/input_day_04.txt");
// let d5_input = include_str!("../assets/input_day_05.txt");
// let d6_input = include_str!("../assets/input_day_06.txt");
// let d7_input = include_str!("../assets/input_day_07.txt");
// let d10_input = include_str!("../assets/input_day_10.txt");
let d11_input = include_str!("../assets/input_day_11.txt");

println!("day 1 - part 1: {}", day_01::part_one(&d1_input));
println!("day 1 - part 1 zip: {}", day_01::part_one_zip(&d1_input));
println!("day 1 - part 2: {}", day_01::part_two(&d1_input));
println!(
"day 1 - part 2 filter: {}",
day_01::part_two_filter(&d1_input)
);
// println!("day 1 - part 1: {}", day_01::part_one(&d1_input));
// println!("day 1 - part 1 zip: {}", day_01::part_one_zip(&d1_input));
// println!("day 1 - part 2: {}", day_01::part_two(&d1_input));
// println!(
// "day 1 - part 2 filter: {}",
// day_01::part_two_filter(&d1_input)
// );

println!("day 2 - part 1: {}", day_02::part_one(&d2_input));
println!("day 2 - part 2: {}", day_02::part_two(&d2_input));
// println!("day 2 - part 1: {}", day_02::part_one(&d2_input));
// println!("day 2 - part 2: {}", day_02::part_two(&d2_input));

println!("day 3 - part 1: {}", day_03::part_one(&d3_input));
println!("day 3 - part 2: {}", day_03::part_two(&d3_input));
println!(
"day 3 - part 2 split: {}",
day_03::part_two_split(&d3_input)
);
// println!("day 3 - part 1: {}", day_03::part_one(&d3_input));
// println!("day 3 - part 2: {}", day_03::part_two(&d3_input));
// println!(
// "day 3 - part 2 split: {}",
// day_03::part_two_split(&d3_input)
// );

println!("day 4 - part 1: {}", day_04::part_one(&d4_input));
println!("day 4 - part 2: {}", day_04::part_two(&d4_input));
// println!("day 4 - part 1: {}", day_04::part_one(&d4_input));
// println!("day 4 - part 2: {}", day_04::part_two(&d4_input));

println!("day 5 - part 1: {}", day_05::part_one(&d5_input));
println!("day 5 - part 2: {}", day_05::part_two(&d5_input));
// println!("day 5 - part 1: {}", day_05::part_one(&d5_input));
// println!("day 5 - part 2: {}", day_05::part_two(&d5_input));

println!("day 6 - part 1: {}", day_06::part_one(&d6_input));
println!("day 6 - part 2: {}", day_06::part_two(&d6_input));
// println!("day 6 - part 1: {}", day_06::part_one(&d6_input));
// println!("day 6 - part 2: {}", day_06::part_two(&d6_input));

println!("day 7 - part 1: {}", day_07::part_one_filter(&d7_input));
println!("day 7 - part 2: {}", day_07::part_two_filter(&d7_input));
// println!("day 7 - part 1: {}", day_07::part_one_filter(&d7_input));
// println!("day 7 - part 2: {}", day_07::part_two_filter(&d7_input));

// println!("day 10 - part 1: {}", day_10::part_one(&d10_input));

println!("day 11 - part 1: {}", day_11::part_one(&d11_input));
}

0 comments on commit 2331d05

Please sign in to comment.