-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.rs
46 lines (38 loc) · 1.21 KB
/
day11.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use cached::proc_macro::cached;
fn parse_input(input: &Vec<String>) -> Vec<u64> {
input
.first()
.unwrap()
.split_whitespace()
.map(|num| num.parse::<u64>().unwrap())
.collect()
}
#[cached]
fn blink(stone: u64, rounds: u64) -> u64 {
if rounds == 0 {
return 1;
}
let rounds_remaining = rounds - 1;
match stone {
0 => blink(1, rounds_remaining),
_ => match split_stone(stone) {
Some((left_half, right_half)) => blink(left_half, rounds_remaining) + blink(right_half, rounds_remaining),
None => blink(stone * 2024, rounds_remaining),
},
}
}
fn split_stone(engraving: u64) -> Option<(u64, u64)> {
let engraving_str = engraving.to_string();
if engraving_str.len() % 2 != 0 {
return None;
}
let middle = engraving_str.len() / 2;
let (left, right) = engraving_str.split_at(middle);
Some((left.parse().unwrap(), right.parse().unwrap()))
}
pub fn part1(input: &Vec<String>) -> i64 {
parse_input(input).iter().map(|&stone| blink(stone, 25)).sum::<u64>() as i64
}
pub fn part2(input: &Vec<String>) -> i64 {
parse_input(input).iter().map(|&stone| blink(stone, 75)).sum::<u64>() as i64
}