Skip to content

Commit

Permalink
d11 p2
Browse files Browse the repository at this point in the history
  • Loading branch information
wiktoriavh committed Dec 11, 2024
1 parent 189eb48 commit 2960b89
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
38 changes: 37 additions & 1 deletion src/11.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function solution(input: string): { part1: string; part2: string } {

const lines = input.split(" ");
const totalBlinkAmount = 25;
const totalBlinkAmount2 = 75;
let stones: string[] = [...lines];

for (let i = 0; i < totalBlinkAmount; i++) {
Expand All @@ -20,9 +21,44 @@ function solution(input: string): { part1: string; part2: string } {
return (Number(line) * 2024).toString();
});
}

result1 = stones.length;

const cache = new Map<string, number>();

function ans(stone: number, iter: number): number {
if (iter === 0) {
return 1;
}

if (cache.has(`${stone},${iter}`)) {
return cache.get(`${stone},${iter}`)!;
}

let res = 0;

if (stone === 0) {
res = ans(1, iter - 1);
} else if (stone.toString().length % 2 === 0) {
const st = stone.toString();
const half = st.length / 2;
const firstHalf = st.slice(0, half);
const secondHalf = st.slice(half);

res = 0;
res += ans(Number(firstHalf), iter - 1);
res += ans(Number(secondHalf), iter - 1);
} else {
res = ans(stone * 2024, iter - 1);
}
cache.set(`${stone},${iter}`, res);

return cache.get(`${stone},${iter}`)!;
}

lines.map(Number).forEach((line) => {
result2 += ans(line, totalBlinkAmount2);
});

return {
part1: result1.toString(),
part2: result2.toString(),
Expand Down
2 changes: 1 addition & 1 deletion test/11.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const EXAMPLE_1 = `125 17`;
const RESULT_1 = "55312";

const EXAMPLE_2 = EXAMPLE_1;
const RESULT_2 = "31";
const RESULT_2 = "65601038650482";

describe("Day 11", () => {
test("Part 1", () => {
Expand Down

0 comments on commit 2960b89

Please sign in to comment.