-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
4329 385 0 1444386 600463 19 1 56615 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use aoc_runner_derive::{aoc, aoc_generator}; | ||
use memoize::memoize; | ||
|
||
#[aoc_generator(day11)] | ||
pub fn input_generator(input: &str) -> Vec<usize> { | ||
input.split(" ").map(|s| s.parse().unwrap()).collect() | ||
} | ||
|
||
// pub fn blink(stones: Vec<usize>, counter: usize, limit: usize) -> Vec<usize> { | ||
// if counter == limit { | ||
// return stones; | ||
// } | ||
// let stones = stones.iter().fold(vec![], |mut acc, stone| { | ||
// if *stone == 0 { | ||
// acc.push(1); | ||
// return acc; | ||
// } | ||
// let mut div = *stone; | ||
// let mut count = 0; | ||
// while div > 0 { | ||
// div /= 10; | ||
// count += 1; | ||
// } | ||
// if count % 2 == 0 { | ||
// acc.push(*stone / 10_usize.pow(count / 2)); | ||
// acc.push(*stone % 10_usize.pow(count / 2)); | ||
// return acc; | ||
// } | ||
// acc.push(2024 * *stone); | ||
// acc | ||
// }); | ||
// blink(stones, counter + 1, limit) | ||
// } | ||
|
||
// #[aoc(day11, part1)] | ||
// pub fn part1(input: &[usize]) -> usize { | ||
// blink(input.to_vec(), 0, 25).len() | ||
// } | ||
|
||
#[memoize] | ||
pub fn blink(stone: usize, counter: usize, limit: usize) -> usize { | ||
let mut stack = 0; | ||
if counter == limit { | ||
return 1; | ||
} | ||
|
||
if stone == 0 { | ||
stack += blink(1, counter + 1, limit); | ||
} else { | ||
let mut div = stone; | ||
let mut count = 0; | ||
while div > 0 { | ||
div /= 10; | ||
count += 1; | ||
} | ||
if count % 2 == 0 { | ||
stack += blink(stone / 10_usize.pow(count / 2), counter + 1, limit); | ||
stack += blink(stone % 10_usize.pow(count / 2), counter + 1, limit); | ||
} else { | ||
stack += blink(stone * 2024, counter + 1, limit); | ||
} | ||
} | ||
stack | ||
} | ||
|
||
#[aoc(day11, part1)] | ||
pub fn part1(input: &[usize]) -> usize { | ||
let mut lenght = 0; | ||
for stone in input { | ||
lenght += blink(*stone, 0, 25); | ||
} | ||
lenght | ||
} | ||
|
||
#[aoc(day11, part2)] | ||
pub fn part2(input: &[usize]) -> usize { | ||
let mut lenght = 0; | ||
for stone in input { | ||
lenght += blink(*stone, 0, 75); | ||
} | ||
lenght | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
const INPUT: &str = "125 17"; | ||
|
||
#[test] | ||
fn test_part1() { | ||
assert_eq!(55312, part1(&input_generator(INPUT))) | ||
} | ||
|
||
#[test] | ||
fn test_part2() { | ||
assert_eq!(55312, part2(&input_generator(INPUT))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
use aoc_runner_derive::aoc_lib; | ||
|
||
mod day1; | ||
mod day11; | ||
mod day2; | ||
mod day3; | ||
mod day4; | ||
mod day5; | ||
mod day6; | ||
mod day7; | ||
mod day8; | ||
|
||
aoc_lib! { year = 2024 } |