Skip to content

Commit

Permalink
Add day11
Browse files Browse the repository at this point in the history
  • Loading branch information
qselle committed Dec 11, 2024
1 parent 7f2ff77 commit a4bb2ba
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 1 deletion.
100 changes: 100 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ edition = "2021"
[dependencies]
aoc-runner = "0.3.0"
aoc-runner-derive = "0.3.0"
memoize = "0.4.2"
regex = "1.11.1"
1 change: 1 addition & 0 deletions input/2024/day11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4329 385 0 1444386 600463 19 1 56615
101 changes: 101 additions & 0 deletions src/day11.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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()
}

#[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)))
}
}

// First implementation: too slow and no memoization possible.

// 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()
// }
2 changes: 1 addition & 1 deletion src/lib.rs
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 }

0 comments on commit a4bb2ba

Please sign in to comment.