Skip to content

Commit

Permalink
day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesPatrickGill committed Dec 12, 2024
1 parent 2370064 commit 338957c
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
| [Day 8](./src/bin/08.rs) | `36.5µs` | `116.8µs` |
| [Day 9](./src/bin/09.rs) | `80.1µs` | `258.4ms` |
| [Day 10](./src/bin/10.rs) | `464.5µs` | `28.4µs` |
| [Day 11](./src/bin/11.rs) | `179.0µs` | `9.2ms` |

**Total: 294.35ms**
**Total: 303.73ms**
<!--- benchmarking table --->
1 change: 1 addition & 0 deletions data/examples/11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
125 17
103 changes: 103 additions & 0 deletions src/bin/11.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use std::collections::HashMap;

advent_of_code::solution!(11);

pub fn part_one(input: &str) -> Option<u64> {
let mut lookup = HashMap::new();
let stones: u64 = input
.trim()
.split(" ")
.map(|num_str| {
let curr_stone = num_str.parse().unwrap();
blink(&mut lookup, curr_stone, 0)
})
.sum();
Some(stones)
}

#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)]
struct LookupKey(u64, u8);

fn blink(lookup: &mut HashMap<LookupKey, u64>, curr_stone: u64, curr_step: u8) -> u64 {
// Break case
if curr_step == 25 {
return 1;
}

// Check Lookup
if let Some(&cached) = lookup.get(&LookupKey(curr_stone, curr_step)) {
return cached;
};

let res = if curr_stone == 0 {
blink(lookup, 1, curr_step + 1)
} else if curr_stone.to_string().len() % 2 == 0 {
let num_str = curr_stone.to_string();
let (l, r) = num_str.split_at(num_str.len() / 2);
blink(lookup, l.parse::<u64>().unwrap(), curr_step + 1)
+ blink(lookup, r.parse::<u64>().unwrap(), curr_step + 1)
} else {
blink(lookup, curr_stone * 2024, curr_step + 1)
};

lookup.insert(LookupKey(curr_stone, curr_step), res);

res
}

pub fn part_two(input: &str) -> Option<u64> {
let mut lookup = HashMap::new();
let stones: u64 = input
.trim()
.split(" ")
.map(|num_str| {
let curr_stone = num_str.parse().unwrap();
blink2(&mut lookup, curr_stone, 0)
})
.sum();
Some(stones)
}

fn blink2(lookup: &mut HashMap<LookupKey, u64>, curr_stone: u64, curr_step: u8) -> u64 {
// Break case
if curr_step == 75 {
return 1;
}

// Check Lookup
if let Some(&cached) = lookup.get(&LookupKey(curr_stone, curr_step)) {
return cached;
};

let res = if curr_stone == 0 {
blink2(lookup, 1, curr_step + 1)
} else if curr_stone.to_string().len() % 2 == 0 {
let num_str = curr_stone.to_string();
let (l, r) = num_str.split_at(num_str.len() / 2);
blink2(lookup, l.parse::<u64>().unwrap(), curr_step + 1)
+ blink2(lookup, r.parse::<u64>().unwrap(), curr_step + 1)
} else {
blink2(lookup, curr_stone * 2024, curr_step + 1)
};

lookup.insert(LookupKey(curr_stone, curr_step), res);

res
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(55312));
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
}
}

0 comments on commit 338957c

Please sign in to comment.