day11: don't use aoc_generator

This commit is contained in:
Keenan Tims 2024-12-11 18:43:57 -08:00
parent 50b6d045e7
commit de535303d4
Signed by: ktims
GPG Key ID: 11230674D69038D4

View File

@ -1,4 +1,4 @@
use aoc_runner_derive::{aoc, aoc_generator}; use aoc_runner_derive::aoc;
use itertools::Itertools; use itertools::Itertools;
use std::collections::HashMap; use std::collections::HashMap;
use std::iter::repeat; use std::iter::repeat;
@ -26,7 +26,6 @@ impl From<&str> for Stones {
} }
} }
#[aoc_generator(day11)]
fn parse(input: &str) -> Stones { fn parse(input: &str) -> Stones {
Stones::from(input) Stones::from(input)
} }
@ -47,8 +46,8 @@ impl Stone {
} }
fn count_blinks(stone: &Stone, blink: usize, cache: &mut Vec<CacheType>) -> IntType { fn count_blinks(stone: &Stone, blink: usize, cache: &mut Vec<CacheType>) -> IntType {
if cache[blink].contains_key(&stone) { if cache[blink].contains_key(stone) {
return cache[blink][&stone].clone(); return cache[blink][stone];
} }
let stones = stone.blink_once(); let stones = stone.blink_once();
let result = if blink == 0 { let result = if blink == 0 {
@ -63,10 +62,10 @@ fn count_blinks(stone: &Stone, blink: usize, cache: &mut Vec<CacheType>) -> IntT
} }
}; };
cache[blink].insert(stone.clone(), result); cache[blink].insert(stone.clone(), result);
cache[blink][&stone].clone() cache[blink][stone]
} }
fn blink_stones(stones: &Stones, blinks: usize) -> IntType { fn blink_stones(stones: Stones, blinks: usize) -> IntType {
let mut cache = Vec::from_iter(repeat(CacheType::new()).take(blinks)); let mut cache = Vec::from_iter(repeat(CacheType::new()).take(blinks));
stones stones
.0 .0
@ -76,12 +75,14 @@ fn blink_stones(stones: &Stones, blinks: usize) -> IntType {
} }
#[aoc(day11, part1)] #[aoc(day11, part1)]
fn part1(stones: &Stones) -> IntType { pub fn part1(input: &str) -> IntType {
let stones = parse(input);
blink_stones(stones, 25) blink_stones(stones, 25)
} }
#[aoc(day11, part2)] #[aoc(day11, part2)]
fn part2(stones: &Stones) -> IntType { pub fn part2(input: &str) -> IntType {
let stones = parse(input);
blink_stones(stones, 75) blink_stones(stones, 75)
} }
@ -92,11 +93,11 @@ mod tests {
#[test] #[test]
fn part1_example() { fn part1_example() {
assert_eq!(part1(&parse(EXAMPLE)), 55312); assert_eq!(part1(EXAMPLE), 55312);
} }
#[test] #[test]
fn part2_example() { fn part2_example() {
assert_eq!(part2(&parse(EXAMPLE)), 65601038650482); assert_eq!(part2(EXAMPLE), 65601038650482);
} }
} }