Skip to content

Commit 51a00d7

Browse files
committed
Add day11
1 parent 7f2ff77 commit 51a00d7

File tree

5 files changed

+202
-1
lines changed

5 files changed

+202
-1
lines changed

Cargo.lock

Lines changed: 100 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ edition = "2021"
66
[dependencies]
77
aoc-runner = "0.3.0"
88
aoc-runner-derive = "0.3.0"
9+
memoize = "0.4.2"
910
regex = "1.11.1"

input/2024/day11.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4329 385 0 1444386 600463 19 1 56615

src/day11.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
use aoc_runner_derive::{aoc, aoc_generator};
2+
use memoize::memoize;
3+
4+
#[aoc_generator(day11)]
5+
pub fn input_generator(input: &str) -> Vec<usize> {
6+
input.split(" ").map(|s| s.parse().unwrap()).collect()
7+
}
8+
9+
// pub fn blink_part1(stones: Vec<usize>, counter: usize, limit: usize) -> Vec<usize> {
10+
// if counter == limit {
11+
// return stones;
12+
// }
13+
// let stones = stones.iter().fold(vec![], |mut acc, stone| {
14+
// if *stone == 0 {
15+
// acc.push(1);
16+
// return acc;
17+
// }
18+
// let mut div = *stone;
19+
// let mut count = 0;
20+
// while div > 0 {
21+
// div /= 10;
22+
// count += 1;
23+
// }
24+
// if count % 2 == 0 {
25+
// acc.push(*stone / 10_usize.pow(count / 2));
26+
// acc.push(*stone % 10_usize.pow(count / 2));
27+
// return acc;
28+
// }
29+
// acc.push(2024 * *stone);
30+
// acc
31+
// });
32+
// blink_part1(stones, counter + 1, limit)
33+
// }
34+
35+
// #[aoc(day11, part1)]
36+
// pub fn part1(input: &[usize]) -> usize {
37+
// blink_part1(input.to_vec(), 0, 25).len()
38+
// }
39+
40+
#[memoize]
41+
pub fn blink(stone: usize, counter: usize, limit: usize) -> usize {
42+
let mut stack = 0;
43+
if counter == limit {
44+
return 1;
45+
}
46+
47+
if stone == 0 {
48+
stack += blink(1, counter + 1, limit);
49+
} else {
50+
let mut div = stone;
51+
let mut count = 0;
52+
while div > 0 {
53+
div /= 10;
54+
count += 1;
55+
}
56+
if count % 2 == 0 {
57+
stack += blink(stone / 10_usize.pow(count / 2), counter + 1, limit);
58+
stack += blink(stone % 10_usize.pow(count / 2), counter + 1, limit);
59+
} else {
60+
stack += blink(stone * 2024, counter + 1, limit);
61+
}
62+
}
63+
stack
64+
}
65+
66+
#[aoc(day11, part1)]
67+
pub fn part1(input: &[usize]) -> usize {
68+
let mut lenght = 0;
69+
for stone in input {
70+
lenght += blink(*stone, 0, 25);
71+
}
72+
lenght
73+
}
74+
75+
#[aoc(day11, part2)]
76+
pub fn part2(input: &[usize]) -> usize {
77+
let mut lenght = 0;
78+
for stone in input {
79+
lenght += blink(*stone, 0, 75);
80+
}
81+
lenght
82+
}
83+
84+
#[cfg(test)]
85+
mod tests {
86+
use super::*;
87+
88+
const INPUT: &str = "125 17";
89+
90+
#[test]
91+
fn test_part1() {
92+
assert_eq!(55312, part1(&input_generator(INPUT)))
93+
}
94+
95+
#[test]
96+
fn test_part2() {
97+
assert_eq!(55312, part2(&input_generator(INPUT)))
98+
}
99+
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use aoc_runner_derive::aoc_lib;
22

33
mod day1;
4+
mod day11;
45
mod day2;
56
mod day3;
67
mod day4;
78
mod day5;
89
mod day6;
910
mod day7;
10-
mod day8;
1111

1212
aoc_lib! { year = 2024 }

0 commit comments

Comments
 (0)