Skip to content

Commit

Permalink
Rust/2024/22: add solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Defelo committed Dec 22, 2024
1 parent 2fcd192 commit 62ade6a
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions Rust/2024/22.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#![feature(test)]

use std::sync::atomic::{AtomicU64, Ordering};

use itertools::Itertools;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

type Input = Vec<u64>;

fn setup(input: &str) -> Input {
input.trim().lines().map(|l| l.parse().unwrap()).collect()
}

fn evolve(secret: u64) -> impl Iterator<Item = u64> {
std::iter::successors(Some(secret), |&num| {
let num = ((num << 6) ^ num) & 0xFFFFFF;
let num = ((num >> 5) ^ num) & 0xFFFFFF;
let num = ((num << 11) ^ num) & 0xFFFFFF;
Some(num)
})
}

fn diffs_to_idx(diffs: [i64; 4]) -> usize {
diffs.into_iter().fold(0, |idx, x| {
debug_assert!((-9..=9).contains(&x));
idx * 19 + (x + 9) as usize
})
}

fn part1(input: &Input) -> u64 {
input
.par_iter()
.map(|&secret| evolve(secret).nth(2000).unwrap())
.sum()
}

fn part2(input: &Input) -> u64 {
let sequences = (0..19usize.pow(4))
.map(|_| AtomicU64::new(0))
.collect::<Vec<_>>();

input.par_iter().for_each(|&secret| {
let mut seen = [0u64; 19usize.pow(4).div_ceil(64)];
evolve(secret)
.map(|num| num % 10)
.tuple_windows()
.map(|(a, b)| (b as i64 - a as i64, b))
.take(2000)
.tuple_windows()
.map(|((a, _), (b, _), (c, _), (d, price))| ((a, b, c, d), price))
.for_each(|(seq, price)| {
let idx = diffs_to_idx(seq.into());
if seen[idx / 64] & (1 << (idx % 64)) != 0 {
return;
}
seen[idx / 64] |= 1 << (idx % 64);
sequences[idx].fetch_add(price, Ordering::Relaxed);
});
});

sequences.into_iter().map(|x| x.into_inner()).max().unwrap()
}

aoc::main!(2024, 22, ex: 1, 2);
2 changes: 1 addition & 1 deletion Rust/2024/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ aoc::year! {
"19.rs",
"20.rs",
"21.rs",
// "22.rs",
"22.rs",
// "23.rs",
// "24.rs",
// "25.rs",
Expand Down
3 changes: 3 additions & 0 deletions Rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,6 @@ path = "2024/20.rs"
[[bin]]
name = "2024_21"
path = "2024/21.rs"
[[bin]]
name = "2024_22"
path = "2024/22.rs"
4 changes: 4 additions & 0 deletions examples/2024/22/1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1
10
100
2024
1 change: 1 addition & 0 deletions examples/2024/22/1.1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
37327623
1 change: 1 addition & 0 deletions examples/2024/22/1.2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
24
4 changes: 4 additions & 0 deletions examples/2024/22/2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1
2
3
2024
1 change: 1 addition & 0 deletions examples/2024/22/2.1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
37990510
1 change: 1 addition & 0 deletions examples/2024/22/2.2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
23

0 comments on commit 62ade6a

Please sign in to comment.