Skip to content

Commit

Permalink
2024-01
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Dec 1, 2024
1 parent cc8fab0 commit 287e2dc
Show file tree
Hide file tree
Showing 3 changed files with 1,041 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/core/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fs::read_to_string;

pub fn criterion_benchmark(c: &mut Criterion) {
#![allow(clippy::unwrap_used)]
for year in 2015..=2023 {
for year in 2015..=2024 {
let start_day = 1;
let end_day = 25;
for day in start_day..=end_day {
Expand Down
48 changes: 40 additions & 8 deletions crates/core/src/year2024/day01.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
use crate::input::Input;
use crate::{
common::array_stack::ArrayStack,
input::{on_error, Input},
};

pub const fn solve(_input: &Input) -> Result<u32, String> {
Ok(0)
pub fn solve(input: &Input) -> Result<u64, String> {
let mut left_list = ArrayStack::<1024, u32>::new();
let mut right_list = ArrayStack::<1024, u32>::new();

for line in input.text.lines() {
let (l, r) = line.split_once(" ").ok_or_else(on_error)?;
left_list.push(l.parse().map_err(|_| on_error())?)?;
right_list.push(r.parse().map_err(|_| on_error())?)?;
}

Ok(if input.is_part_one() {
right_list.slice_mut().sort_unstable();
left_list.slice_mut().sort_unstable();
left_list
.slice()
.iter()
.zip(right_list.slice().iter())
.map(|(l, &r)| l.abs_diff(r))
.sum::<u32>() as u64
} else {
left_list
.slice()
.iter()
.map(|&l| u64::from(l) * right_list.slice().iter().filter(|&&r| r == l).count() as u64)
.sum::<u64>()
})
}

#[test]
pub fn tests() {
use crate::input::{test_part_one_no_allocations, test_part_two_no_allocations};

let test_input = "";
test_part_one_no_allocations!(test_input => 0);
test_part_two_no_allocations!(test_input => 0);
let test_input = "3 4
4 3
2 5
1 3
3 9
3 3";
test_part_one_no_allocations!(test_input => 11);
test_part_two_no_allocations!(test_input => 31);

let real_input = include_str!("day01_input.txt");
test_part_one_no_allocations!(real_input => 0);
test_part_two_no_allocations!(real_input => 0);
test_part_one_no_allocations!(real_input => 1_882_714);
test_part_two_no_allocations!(real_input => 19_437_052);
}
Loading

1 comment on commit 287e2dc

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@                     Benchmark Difference                     @@
#      Name   Old (instructions)   New (instructions)   Change (%)
   2023_1_1            1,484,904            1,484,904            0
   2023_1_2            1,457,486            1,457,486            0
   2023_2_1              565,166              565,166            0
   2023_2_2              538,291              538,291            0
   2023_3_1              831,300              831,300            0
   2023_3_2              627,081              627,081            0
   2023_4_1              911,485              911,485            0
   2023_4_2              922,701              922,701            0
   2023_5_1              638,297              638,297            0
   2023_5_2              773,139              773,139            0
   2023_6_1                3,686                3,686            0
   2023_6_2                2,655                2,655            0
   2023_7_1              661,073              661,073            0
   2023_7_2              657,249              657,249            0
   2023_8_1            1,174,409            1,174,661            0
   2023_8_2            2,580,105            2,580,225            0
   2023_9_1              799,599              799,599            0
   2023_9_2              811,324              811,324            0
  2023_10_1            2,950,347            2,950,347            0
  2023_10_2            3,513,752            3,513,752            0
  2023_11_1            1,741,784            1,741,784            0
  2023_11_2            1,741,808            1,741,808            0
  2023_12_1            4,374,269            4,374,269            0
  2023_12_2           68,540,595           68,540,595            0
  2023_13_1              583,261              583,261            0
  2023_13_2              584,317              584,317            0
  2023_14_1              839,816              839,816            0
  2023_14_2          289,510,494          289,510,494            0
  2023_15_1              807,775              807,775            0
  2023_15_2            1,087,804            1,087,804            0
  2023_16_1            1,074,625            1,074,611            0
  2023_16_2          220,133,115          220,130,028            0
  2023_17_1          216,844,222          216,844,222            0
  2023_17_2          666,065,705          666,065,705            0
  2023_18_1              368,082              368,082            0
  2023_18_2              438,373              438,373            0
  2023_19_1            3,006,478            3,006,454            0
  2023_19_2            1,918,802            1,918,778            0
  2023_20_1            4,637,723            4,637,735            0
  2023_20_2           18,235,135           18,235,147            0
  2023_21_1            3,137,778            3,137,778            0
  2023_21_2            4,545,451            4,545,451            0
  2023_22_1           10,749,692           10,749,692            0
  2023_22_2          291,836,479          291,836,479            0
  2023_23_1            1,693,578            1,693,610            0
  2023_23_2        1,129,554,238        1,129,554,270            0
  2023_24_1            3,666,373            3,666,389            0
  2023_24_2              982,477              982,493            0
  2023_25_1            2,376,198            2,376,198            0
Benchmark Instructions (count) Instructions (%)
2023_23_2 1,129,554,270 38.0
2023_17_2 666,065,705 22.4
2023_22_2 291,836,479 9.8
2023_14_2 289,510,494 9.7
2023_16_2 220,130,028 7.4
2023_17_1 216,844,222 7.3
2023_12_2 68,540,595 2.3
2023_20_2 18,235,147 0.6
2023_22_1 10,749,692 0.4
2023_20_1 4,637,735 0.2
2023_21_2 4,545,451 0.2
2023_12_1 4,374,269 0.1
2023_24_1 3,666,389 0.1
2023_10_2 3,513,752 0.1
2023_21_1 3,137,778 0.1
2023_19_1 3,006,454 0.1
2023_10_1 2,950,347 0.1
2023_8_2 2,580,225 0.1
2023_25_1 2,376,198 0.1
2023_19_2 1,918,778 0.1
2023_11_2 1,741,808 0.1
2023_11_1 1,741,784 0.1
2023_23_1 1,693,610 0.1
2023_1_1 1,484,904 0.0
2023_1_2 1,457,486 0.0
2023_8_1 1,174,661 0.0
2023_15_2 1,087,804 0.0
2023_16_1 1,074,611 0.0
2023_24_2 982,493 0.0
2023_4_2 922,701 0.0
2023_4_1 911,485 0.0
2023_14_1 839,816 0.0
2023_3_1 831,300 0.0
2023_9_2 811,324 0.0
2023_15_1 807,775 0.0
2023_9_1 799,599 0.0
2023_5_2 773,139 0.0
2023_7_1 661,073 0.0
2023_7_2 657,249 0.0
2023_5_1 638,297 0.0
2023_3_2 627,081 0.0
2023_13_2 584,317 0.0
2023_13_1 583,261 0.0
2023_2_1 565,166 0.0
2023_2_2 538,291 0.0
2023_18_2 438,373 0.0
2023_18_1 368,082 0.0
2023_6_1 3,686 0.0
2023_6_2 2,655 0.0

Please sign in to comment.