Skip to content

Commit e33fe81

Browse files
committed
use memoize macro
1 parent 91e4047 commit e33fe81

File tree

3 files changed

+24
-38
lines changed

3 files changed

+24
-38
lines changed

src/bin/11.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,42 @@
11
advent_of_code::solution!(11);
22

3+
use advent_of_code_macros::memoize;
4+
35
use advent_of_code::majcn::math::*;
4-
use advent_of_code::maneatingape::hash::*;
56
use advent_of_code::maneatingape::parse::*;
67

78
fn parse_data(input: &str) -> Vec<u32> {
89
input.iter_unsigned().collect()
910
}
1011

11-
fn calculate_step(v: u64) -> [Option<u64>; 2] {
12+
fn calculate_step(v: u64) -> Vec<u64> {
1213
if v == 0 {
13-
return [Some(1), None];
14+
return vec![1];
1415
}
1516

1617
let n = v.count_digits() as u32;
1718
if n % 2 == 0 {
1819
let split_value = 10_u64.pow(n / 2);
19-
return [Some(v / split_value), Some(v % split_value)];
20+
return vec![v / split_value, v % split_value];
2021
}
2122

22-
[Some(v * 2024), None]
23+
vec![v * 2024]
2324
}
2425

25-
fn calculate<const N: usize>(v: u64, i: usize, cache: &mut FastMap<(usize, u64), u64>) -> u64 {
26-
if i == N {
26+
#[memoize]
27+
fn calculate(v: u64, n: usize) -> u64 {
28+
if n == 0 {
2729
return 1;
2830
}
2931

30-
if let Some(cached_result) = cache.get(&(i, v)) {
31-
return *cached_result;
32-
}
33-
34-
let result = calculate_step(v)
32+
calculate_step(v)
3533
.into_iter()
36-
.filter_map(|x| Some(calculate::<N>(x?, i + 1, cache)))
37-
.sum();
38-
39-
cache.insert((i, v), result);
40-
41-
result
34+
.map(|x| calculate(x, n - 1))
35+
.sum()
4236
}
4337

4438
fn part_x<const N: usize>(data: Vec<u32>) -> u64 {
45-
let mut cache = FastMap::new();
46-
47-
data.into_iter()
48-
.map(|x| calculate::<N>(x as u64, 0, &mut cache))
49-
.sum()
39+
data.into_iter().map(|x| calculate(x as u64, N)).sum()
5040
}
5141

5242
pub fn part_one(input: &str) -> Option<u64> {

src/bin/19.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
advent_of_code::solution!(19);
22

3-
use advent_of_code::maneatingape::hash::*;
3+
use advent_of_code_macros::memoize;
44

55
fn parse_data(input: &str) -> (Vec<&str>, Vec<&str>) {
66
let (left, right) = input.split_once("\n\n").unwrap();
@@ -23,24 +23,21 @@ fn validate_message(message: &str, towels: &[&str]) -> bool {
2323
.any(|towel| validate_message(&message[towel.len()..], towels))
2424
}
2525

26-
fn count_valid_message(cache: &mut FastMap<String, u64>, message: &str, towels: &[&str]) -> u64 {
26+
fn count_valid_message_cache_key(message: &str, _: &[&str]) -> String {
27+
String::from(message)
28+
}
29+
30+
#[memoize(key_function = "count_valid_message_cache_key -> String")]
31+
fn count_valid_message(message: &str, towels: &[&str]) -> u64 {
2732
if message.is_empty() {
2833
return 1;
2934
}
3035

31-
if let Some(&result) = cache.get(message) {
32-
return result;
33-
}
34-
3536
towels
3637
.iter()
3738
.filter(|towel| message.len() >= towel.len())
3839
.filter(|towel| &&&message[..towel.len()] == towel)
39-
.map(|towel| {
40-
let result = count_valid_message(cache, &message[towel.len()..], towels);
41-
cache.insert(String::from(&message[towel.len()..]), result);
42-
result
43-
})
40+
.map(|towel| count_valid_message(&message[towel.len()..], towels))
4441
.sum()
4542
}
4643

@@ -58,11 +55,9 @@ pub fn part_one(input: &str) -> Option<u32> {
5855
pub fn part_two(input: &str) -> Option<u64> {
5956
let (towels, messages) = parse_data(input);
6057

61-
let mut cache = FastMap::new();
62-
6358
let result = messages
6459
.into_iter()
65-
.map(|message| count_valid_message(&mut cache, message, &towels))
60+
.map(|message| count_valid_message(message, &towels))
6661
.sum();
6762

6863
Some(result)

src/bin/21.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
advent_of_code::solution!(21);
22

3+
use advent_of_code_macros::memoize;
4+
35
use advent_of_code::maneatingape::heap::*;
46
use advent_of_code::maneatingape::parse::*;
5-
use advent_of_code_macros::memoize;
67

78
fn parse_data(input: &str) -> Vec<u32> {
89
input.lines().map(|line| line.unsigned()).collect()

0 commit comments

Comments
 (0)