Skip to content

Commit

Permalink
2024-{23,24,25}-1
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Dec 25, 2024
1 parent b7fbc8c commit 94c331b
Show file tree
Hide file tree
Showing 6 changed files with 7,992 additions and 25 deletions.
99 changes: 91 additions & 8 deletions crates/core/src/year2024/day23.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,101 @@
use crate::input::Input;
use std::collections::{HashMap, HashSet};

pub const fn solve(_input: &Input) -> Result<u32, String> {
Ok(0)
use crate::{
common::{array_stack::ArrayStack, id_assigner::IdAssigner},
input::{on_error, Input},
year2017::disjoint_set::DisjointSet,
};

pub fn solve(input: &Input) -> Result<u32, String> {
let mut with_t = ArrayStack::<1000, u16>::new();
let mut to_adjacency = HashMap::new();

for line in input.text.lines() {
let (s1, s2) = line.split_once('-').ok_or_else(on_error)?;
let (n1, n2) = (to_num(s1), to_num(s2));

to_adjacency.entry(n1).or_insert_with(Vec::new).push(n2);
to_adjacency.entry(n2).or_insert_with(Vec::new).push(n1);

if s1.starts_with('t') {
with_t.push(n1)?;
}
if s2.starts_with('t') {
with_t.push(n2)?;
}
}

let mut already_considered = HashSet::new();
let mut result = 0;
let slice = with_t.slice_mut();
slice.sort_unstable();
let mut last = u16::MAX;
for n in slice {
if *n != last {
let adjacency_list = to_adjacency.get(n).unwrap();
for (i, x) in adjacency_list.iter().enumerate() {
for (j, y) in adjacency_list.iter().enumerate().skip(i + 1) {
if to_adjacency.get(x).unwrap().iter().any(|e| e == y) {
let mut aa = [*n, *x, *y];
aa.sort_unstable();
if already_considered.insert(aa) {
result += 1;
}
}
}
}
last = *n;
}
}

Ok(result)
}

fn to_num(p: &str) -> u16 {
let p = p.as_bytes();
u16::from(p[0] - b'a') + 29 * u16::from(p[1] - b'a')
}

#[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 = "kh-tc
qp-kh
de-cg
ka-co
yn-aq
qp-ub
cg-tb
vc-aq
tb-ka
wh-tc
yn-cg
kh-ub
ta-co
de-co
tc-td
tb-wq
wh-td
ta-ka
td-qp
aq-cg
wq-ub
ub-vc
de-ta
wq-aq
wq-vc
wh-yn
ka-de
kh-ta
co-tc
wh-qp
tb-vc
td-yn";
test_part_one_no_allocations!(test_input => 7);
//test_part_two_no_allocations!(test_input => 0);

let real_input = include_str!("day23_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 => 1175);
//test_part_two_no_allocations!(real_input => 0);
}
Loading

1 comment on commit 94c331b

@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 (%)
+ 2024_23_1           10,000,000            3,865,820          -61
+ 2024_23_2           10,000,000            3,864,548          -61
+ 2024_24_1           10,000,000            1,090,182          -89
+ 2024_24_2           10,000,000            1,090,182          -89
+ 2024_25_1           10,000,000            1,011,787          -89
   2024_1_1            1,041,088            1,041,087            0
   2024_1_2            1,070,180            1,070,179            0
   2024_2_1            1,741,514            1,741,515            0
   2024_2_2            1,962,905            1,962,906            0
   2024_3_1              227,379              227,380            0
   2024_3_2              317,752              317,753            0
   2024_4_1            1,213,771            1,213,772            0
   2024_4_2              453,901              453,902            0
   2024_5_1            1,605,046            1,605,047            0
   2024_5_2            1,718,988            1,718,989            0
   2024_6_1              641,454              641,455            0
   2024_6_2          141,310,078          141,310,079            0
   2024_7_1            2,466,893            2,466,892            0
   2024_7_2            3,758,402            3,758,401            0
   2024_8_1               44,405               44,406            0
   2024_8_2               86,862               86,863            0
   2024_9_1              897,540              897,540            0
   2024_9_2            2,497,020            2,497,020            0
  2024_10_1            1,087,487            1,087,488            0
  2024_10_2            1,089,386            1,089,387            0
  2024_11_1            3,848,400            3,848,428            0
  2024_11_2           13,462,556           13,462,438            0
  2024_12_1            7,663,694            7,663,695            0
  2024_12_2            7,942,245            7,942,246            0
  2024_13_1              910,454              910,454            0
  2024_13_2              910,208              910,208            0
  2024_14_1              593,112              593,113            0
  2024_14_2           13,416,623           13,416,624            0
  2024_15_1            1,913,992            1,913,993            0
  2024_15_2           59,667,647           59,667,648            0
  2024_16_1           18,233,703           18,233,704            0
  2024_16_2           18,321,269           18,321,270            0
  2024_17_1               11,725               11,725            0
  2024_17_2              218,087              218,087            0
  2024_18_1            1,103,427            1,103,427            0
  2024_18_2            1,427,027            1,427,027            0
  2024_19_1            2,242,630            2,242,629            0
  2024_19_2            2,242,690            2,242,689            0
  2024_20_1            7,190,091            7,190,092            0
  2024_20_2          241,085,738          241,085,739            0
  2024_21_1              257,460              257,459            0
  2024_21_2            1,868,473            1,868,791            0
  2024_22_1          232,398,347          232,398,346            0
  2024_22_2          359,378,823          359,378,822            0
Benchmark Instructions (count) Instructions (%)
2024_22_2 359,378,822 30.7
2024_20_2 241,085,739 20.6
2024_22_1 232,398,346 19.8
2024_6_2 141,310,079 12.1
2024_15_2 59,667,648 5.1
2024_16_2 18,321,270 1.6
2024_16_1 18,233,704 1.6
2024_11_2 13,462,438 1.1
2024_14_2 13,416,624 1.1
2024_12_2 7,942,246 0.7
2024_12_1 7,663,695 0.7
2024_20_1 7,190,092 0.6
2024_23_1 3,865,820 0.3
2024_23_2 3,864,548 0.3
2024_11_1 3,848,428 0.3
2024_7_2 3,758,401 0.3
2024_9_2 2,497,020 0.2
2024_7_1 2,466,892 0.2
2024_19_2 2,242,689 0.2
2024_19_1 2,242,629 0.2
2024_2_2 1,962,906 0.2
2024_15_1 1,913,993 0.2
2024_21_2 1,868,791 0.2
2024_2_1 1,741,515 0.1
2024_5_2 1,718,989 0.1
2024_5_1 1,605,047 0.1
2024_18_2 1,427,027 0.1
2024_4_1 1,213,772 0.1
2024_18_1 1,103,427 0.1
2024_24_2 1,090,182 0.1
2024_24_1 1,090,182 0.1
2024_10_2 1,089,387 0.1
2024_10_1 1,087,488 0.1
2024_1_2 1,070,179 0.1
2024_1_1 1,041,087 0.1
2024_25_1 1,011,787 0.1
2024_13_1 910,454 0.1
2024_13_2 910,208 0.1
2024_9_1 897,540 0.1
2024_6_1 641,455 0.1
2024_14_1 593,113 0.1
2024_4_2 453,902 0.0
2024_3_2 317,753 0.0
2024_21_1 257,459 0.0
2024_3_1 227,380 0.0
2024_17_2 218,087 0.0
2024_8_2 86,863 0.0
2024_8_1 44,406 0.0
2024_17_1 11,725 0.0

Please sign in to comment.