Skip to content

Commit

Permalink
2024D09: Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
martapanc committed Dec 10, 2024
1 parent 276c46d commit 884eef3
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 7 deletions.
9 changes: 7 additions & 2 deletions 2024/src/2024/day09/day09.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ describe('2024 Day 9', () => {
expect(await part1('input')).toEqual(6435922584968);
});

// 00...111...2...333.44.5555.6666.777.888899
// 0099.111...2...333.44.5555.6666.777.8888..
// 0099.1117772...333.44.5555.6666.....8888..
// 0099.111777244.333....5555.6666.....8888..
// 00992111777.44.333....5555.6666.....8888..
test('Part 2', async () => {
// expect(await part2('testInput1')).toEqual(31);
// expect(await part2('input')).toEqual(29379307);
expect(await part2('testInput1')).toEqual(2858);
expect(await part2('input')).toEqual(6469636832766);
});
});

Expand Down
80 changes: 75 additions & 5 deletions 2024/src/2024/day09/day09.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function part1(inputFile: string) {
}

export async function part2(inputFile: string) {
return await day9(inputFile);
return await day9(inputFile, fragmentAndCalcChecksum2);
}

async function day9(inputFile: string, calcFn?: (line: string) => number) {
Expand Down Expand Up @@ -60,11 +60,77 @@ function fragmentAndCalcChecksum(line: string) {
}
}

return collapseAndCalcChecksum(expandedDisk);
return calcChecksum(expandedDisk);
}

function fragmentAndCalcChecksum2(line: string) {
const expandedDisk = expandDiskMap(line);

const highestBlockIndex = expandedDisk.length - 1;
const highestBlock = expandedDisk[highestBlockIndex];

let blockId: number = Number.parseInt(Object.keys(highestBlock)[0]);

while (blockId > 0) {
const blockIndex = expandedDisk.findIndex(block => Number(Object.keys(block)[0]) === blockId);
const block = expandedDisk[blockIndex];
const blockLength = block?.[blockId]!;

const firstLargeEnoughEmptyIndex = expandedDisk.findIndex((item) => '.' in item && item['.'] as number >= blockLength );
if (firstLargeEnoughEmptyIndex === -1 || firstLargeEnoughEmptyIndex > blockIndex) {
blockId--;
continue;
}
let firstLargeEnoughEmptyBlock = expandedDisk[firstLargeEnoughEmptyIndex];
const [dot, emptyLength] = Object.entries(firstLargeEnoughEmptyBlock)[0];

let newCurrentBlockIndex = blockIndex;

if (blockLength < emptyLength || blockLength === emptyLength) {

if (blockLength < emptyLength) {
firstLargeEnoughEmptyBlock = { [blockId]: blockLength };
expandedDisk[firstLargeEnoughEmptyIndex] = firstLargeEnoughEmptyBlock;

const remainingEmptyBlock = {[dot]: emptyLength - blockLength };
expandedDisk.splice(firstLargeEnoughEmptyIndex + 1, 0, remainingEmptyBlock);

expandedDisk.splice(blockIndex + 1, 1, {'.': blockLength});

// Two blocks (one full, one empty) were created, so bumping block index
newCurrentBlockIndex++;
} else if (blockLength === emptyLength) {
firstLargeEnoughEmptyBlock = { [blockId]: blockLength };
expandedDisk[firstLargeEnoughEmptyIndex] = firstLargeEnoughEmptyBlock;

expandedDisk.splice(blockIndex, 1, {'.': emptyLength});
}

// Merge consecutive empty blocks if newly formed
const prevBlock = expandedDisk[newCurrentBlockIndex - 1];
const currentBlock = expandedDisk[newCurrentBlockIndex];
const nextBlock = expandedDisk[newCurrentBlockIndex + 1];

if (nextBlock && '.' in currentBlock && '.' in nextBlock) {
const nextBlockLength = nextBlock['.'];
expandedDisk.splice(newCurrentBlockIndex, 2, { '.': blockLength + nextBlockLength });
}
if ('.' in prevBlock && '.' in currentBlock) {
const prevBlockLength = prevBlock['.'];
expandedDisk.splice(newCurrentBlockIndex - 1, 2, { '.': prevBlockLength + expandedDisk[newCurrentBlockIndex]['.'] })
}
}

blockId--;
}

return calcChecksum(expandedDisk);
}

type Block = { [key: number | string]: number };

export function expandDiskMap(line: string) {
const list: object[] = [];
const list: Block[] = [];

let blockIdx = 0;
line.split('').forEach((item, index) => {
Expand All @@ -82,14 +148,18 @@ export function expandDiskMap(line: string) {
return list;
}

export function collapseAndCalcChecksum(list: object[]) {
export function calcChecksum(list: object[]) {
let checksum = 0;
let currentPosition = 0;

for (const block of list) {
const [blockId, length] = Object.entries(block)[0];
const id = parseInt(blockId, 10);
const len = length;
if (blockId === '.') {
currentPosition += len;
continue;
}
const id = parseInt(blockId, 10);

for (let i = 0; i < len; i++) {
checksum += id * (currentPosition + i);
Expand Down

0 comments on commit 884eef3

Please sign in to comment.