day11: some cleanup, slight performance

This commit is contained in:
Keenan Tims 2024-12-11 01:20:33 -08:00
parent 2e239681ce
commit 3bfde9fd9b
Signed by: ktims
GPG Key ID: 11230674D69038D4
3 changed files with 22 additions and 88 deletions

46
11/Cargo.lock generated
View File

@ -2,37 +2,11 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
[[package]]
name = "day11"
version = "0.1.0"
dependencies = [
"itertools",
"rayon",
]
[[package]]
@ -49,23 +23,3 @@ checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
dependencies = [
"either",
]
[[package]]
name = "rayon"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]

View File

@ -5,4 +5,3 @@ edition = "2021"
[dependencies]
itertools = "0.13.0"
rayon = "1.10.0"

View File

@ -39,8 +39,18 @@ fn main() {
}
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
struct Stone {
value: u64,
struct Stone(u64);
struct Stones(Vec<Stone>);
impl From<&str> for Stones {
fn from(input: &str) -> Self {
Stones(
input
.split_ascii_whitespace()
.map(|v| Stone(v.parse().unwrap()))
.collect_vec(),
)
}
}
enum BlinkResult {
@ -50,30 +60,16 @@ enum BlinkResult {
impl Stone {
fn blink_once(self) -> BlinkResult {
let n_digits = if self.value == 0 { 1 } else { self.value.ilog10() + 1 };
if self.value == 0 {
BlinkResult::One(Stone { value: 1 })
let n_digits = if self.0 == 0 { 1 } else { self.0.ilog10() + 1 };
if self.0 == 0 {
BlinkResult::One(Stone(1))
} else if n_digits % 2 == 0 {
let parts = (
self.value / 10u64.pow(n_digits / 2),
self.value % 10u64.pow(n_digits / 2),
);
BlinkResult::Two(Stone { value: parts.0 }, Stone { value: parts.1 })
let parts = (self.0 / 10u64.pow(n_digits / 2), self.0 % 10u64.pow(n_digits / 2));
BlinkResult::Two(Stone(parts.0), Stone(parts.1))
} else {
BlinkResult::One(Stone {
value: self.value * 2024,
})
BlinkResult::One(Stone(self.0 * 2024))
}
}
// #[allow(dead_code)]
// fn blink(self, times: usize) -> Vec<Stone> {
// // Used in submitted part 1 solution
// let mut stones = vec![self];
// for _ in 0..times {
// stones = stones.iter().flat_map(|stone| stone.blink_once()).collect();
// }
// stones
// }
}
fn count_blinks(stone: Stone, blink: usize, cache: &mut Vec<HashMap<Stone, u64>>) -> u64 {
@ -96,9 +92,10 @@ fn count_blinks(stone: Stone, blink: usize, cache: &mut Vec<HashMap<Stone, u64>>
result
}
fn blink_stones(stones: &[Stone], blinks: usize) -> u64 {
fn blink_stones(stones: &Stones, blinks: usize) -> u64 {
let mut cache = Vec::from_iter(repeat(HashMap::new()).take(blinks));
stones
.0
.iter()
.map(|stone| count_blinks(*stone, blinks - 1, &mut cache))
.sum()
@ -107,29 +104,13 @@ fn blink_stones(stones: &[Stone], blinks: usize) -> u64 {
// PROBLEM 1 solution
fn problem1<T: BufRead>(mut input: Lines<T>) -> u64 {
let stones = input
.next()
.unwrap()
.unwrap()
.split_ascii_whitespace()
.map(|v| Stone {
value: v.parse().unwrap(),
})
.collect_vec();
let stones = input.next().unwrap().unwrap().as_str().into();
blink_stones(&stones, 25)
}
// PROBLEM 2 solution
fn problem2<T: BufRead>(mut input: Lines<T>) -> u64 {
let stones = input
.next()
.unwrap()
.unwrap()
.split_ascii_whitespace()
.map(|v| Stone {
value: v.parse().unwrap(),
})
.collect_vec();
let stones = input.next().unwrap().unwrap().as_str().into();
blink_stones(&stones, 75)
}