Skip to content

Commit

Permalink
2024D11: Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
martapanc committed Dec 11, 2024
1 parent 557813a commit 30a29c2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
12 changes: 6 additions & 6 deletions 2024/src/2024/day11/day11.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {blink, part1, part2} from "./day11";
import {blinkV1, part1, part2} from "./day11";

const input = "41078 18 7 0 4785508 535256 8154 447";
const testInput1 = "0 1 10 99 999";
Expand All @@ -11,13 +11,13 @@ describe('2024 Day 11', () => {
});

test('Part 2', async () => {
expect(await part2(input)).toEqual(217443);
expect(await part2(input)).toEqual(257246536026785);
});
});

test('blink', () => {
expect(blink(testInput1).join(" ")).toEqual("1 2024 1 0 9 9 2021976")
expect(blink(testInput2).join(" ")).toEqual("253000 1 7")
expect(blink(blink(testInput2)).join(" ")).toEqual("253 0 2024 14168")
expect(blink(blink(blink(testInput2))).join(" ")).toEqual("512072 1 20 24 28676032")
expect(blinkV1(testInput1).join(" ")).toEqual("1 2024 1 0 9 9 2021976")
expect(blinkV1(testInput2).join(" ")).toEqual("253000 1 7")
expect(blinkV1(blinkV1(testInput2)).join(" ")).toEqual("253 0 2024 14168")
expect(blinkV1(blinkV1(blinkV1(testInput2))).join(" ")).toEqual("512072 1 20 24 28676032")
});
45 changes: 31 additions & 14 deletions 2024/src/2024/day11/day11.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@

export async function part1(inputString: string) {
return await day11(inputString, 25);
return day11(inputString, 25);
}

export async function part2(inputString: string) {
return await day11(inputString, 75);
return day11(inputString, 75);
}

async function day11(inputString: string, times: number) {
return calcBlinkOutput(inputString.split(" "), times);
}

function calcBlinkOutput(stones: string[], times: number) {
let expandedStonesCount= 0;
let stoneCounts: Map<string, number> = new Map();

stones.forEach(stone => {
expandedStonesCount += blinkNtimes([stone], times);
})

return expandedStonesCount;
}
stoneCounts.set(stone, (stoneCounts.get(stone) || 0) + 1);
});

function blinkNtimes(stones: string[], times: number) {
let expandedStones = [...stones];
for (let i = 0; i < times; i++) {
expandedStones = blink(expandedStones);
stoneCounts = blink(stoneCounts);
}
return expandedStones.length;

return Array.from(stoneCounts.values())
.reduce((sum, count) => sum + count, 0);
}

function blink(stoneCounts: Map<string, number>): Map<string, number> {
const newStoneCounts: Map<string, number> = new Map();

stoneCounts.forEach((count, stone) => {
if (stone === '0') {
newStoneCounts.set('1', (newStoneCounts.get('1') || 0) + count);
} else if (stone.length % 2 === 0) {
const half = stone.length / 2;
const first = Number.parseInt(stone.slice(0, half)) + '';
const second = Number.parseInt(stone.slice(half)) + '';
newStoneCounts.set(first, (newStoneCounts.get(first) || 0) + count);
newStoneCounts.set(second, (newStoneCounts.get(second) || 0) + count);
} else {
const multiplied = (Number.parseInt(stone) * 2024) + '';
newStoneCounts.set(multiplied, (newStoneCounts.get(multiplied) || 0) + count);
}
});

return newStoneCounts;
}

// Every time you blink, the stones each simultaneously change according to the _first applicable_ rule in this list:
Expand All @@ -36,7 +53,7 @@ function blinkNtimes(stones: string[], times: number) {
// The left half of the digits are engraved on the new left stone, and the right half of the digits are engraved on the new right stone.
// 3. If none of the other rules apply, the stone is replaced by a new stone;
// the old stone's number multiplied by 2024 is engraved on the new stone.
export function blink(stones: string[] | string): string[] {
export function blinkV1(stones: string[] | string): string[] {
const inputStones = typeof stones === 'string' ? stones.split(" ") : stones;
const updatedStones: string[] = [];

Expand Down

0 comments on commit 30a29c2

Please sign in to comment.