diff --git a/README.md b/README.md index 66ce94b..9f5788b 100644 --- a/README.md +++ b/README.md @@ -37,11 +37,11 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www. | [Day 8](./src/bin/08.rs) | `106.2µs` | `-` | | [Day 9](./src/bin/09.rs) | `1.0ms` | `-` | | [Day 10](./src/bin/10.rs) | `10.1ms` | `-` | -| [Day 11](./src/bin/11.rs) | `6.0s` | `-` | +| [Day 11](./src/bin/11.rs) | `1.9s` | `-` | | [Day 12](./src/bin/12.rs) | `4.5ms` | `-` | | [Day 13](./src/bin/13.rs) | `3.1ms` | `-` | -**Total: 6080.39ms** +**Total: 1980.39ms** --- diff --git a/src/bin/11.rs b/src/bin/11.rs index 43557fa..8624f55 100644 --- a/src/bin/11.rs +++ b/src/bin/11.rs @@ -18,33 +18,32 @@ pub fn part_two(_input: &str) -> Option { None } -fn step_stones(stones: &mut Vec, max_steps: usize) { +fn step_stones(stones: &mut Vec, max_steps: usize) { for _step in 0..max_steps { // println!("STEP #{step}: {:?} stones", stones.len()); // stones.clone_from(&process_stones(stones).iter().map(|s| s as &str).collect_vec()); // stones = next_step.clone().iter().map(|s| s as &str).collect_vec(); for idx in (0..stones.len()).rev() { let stone = &stones[idx]; - let stone_length = stone.len(); - if stone == "0" { - stones[idx] = String::from("1"); - } else if stone_length % 2 == 0 { - let mut stone_iter = stone.chars(); - let first_half = stone_iter.by_ref().take(stone_length/2).collect::(); - let second_half = stone_iter.by_ref().take(stone_length/2).collect::().parse::().unwrap().to_string(); + let stone_digits = stone.checked_ilog10().unwrap_or(0) + 1; + if *stone == 0 { + stones[idx] = 1; + } else if stone_digits % 2 == 0 { + let stone_chars = stone.to_string().chars().collect_vec(); + let first_half = stone_chars.iter().take(stone_digits as usize / 2).collect::().parse::().unwrap(); + let second_half = stone_chars.iter().skip(stone_digits as usize / 2).take(stone_digits as usize / 2).collect::().parse::().unwrap(); stones[idx] = first_half; stones.insert(idx + 1, second_half); } else { - let new_stone = stone.parse::().unwrap().mul(2024).to_string(); - stones[idx] = new_stone; + stones[idx] = stone.mul(2024); } } } } -fn vecify_stones(input: &str) -> Vec { - input.strip_suffix("\n").unwrap().split(" ").map(|s| s.to_string()).collect_vec() +fn vecify_stones(input: &str) -> Vec { + input.strip_suffix("\n").unwrap().split(" ").map(|s| s.parse::().unwrap()).collect_vec() } #[cfg(test)]