Skip to content

Commit

Permalink
2024D12: Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
martapanc committed Dec 12, 2024
1 parent e1745ac commit 00a0ad4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
8 changes: 4 additions & 4 deletions 2024/src/2024/day12/day12.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import {part1, part2} from "./day12";

describe('2024 Day 12', () => {
test('Part 1', async () => {
expect(await part1('testInput1')).toEqual(772);
expect(await part1('testInput2')).toEqual(140);
expect(await part1('testInput2')).toEqual(1930);
expect(await part1('input')).toEqual(44444);
expect(await part1('testInput1')).toEqual(140);
expect(await part1('testInput2')).toEqual(772);
expect(await part1('testInput3')).toEqual(1930);
expect(await part1('input')).toEqual(1424006);
});

test('Part 2', async () => {
Expand Down
51 changes: 37 additions & 14 deletions 2024/src/2024/day12/day12.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from "node:path";
import {readInputLineByLine} from "@utils/io";
import {Coord, getNeighborCoords, getNeighbors, Grid, readLinesToGrid} from "@utils/grid";
import * as module from "node:module";
import {Coord, getNeighborCoords, Grid, readLinesToGrid} from "@utils/grid";

export async function part1(inputFile: string) {
return await day12(inputFile, calcAreaAndPerimeter);
Expand All @@ -19,20 +18,44 @@ async function day12(inputFile: string, calcFn?: (grid: Grid) => number) {
}

function calcAreaAndPerimeter(grid: Grid) {
let sum = 0;
const perimeterMap: Map<string, number> = new Map();
const areaMap: Map<string, number> = new Map();

for (const [coord, cell] of grid) {
const neighbors = getNeighbors(Coord.deserialize(coord), grid);
const perimeter = 4 - neighbors.filter(n => n === cell).length;
perimeterMap.set(cell, (perimeterMap.get(cell) || 0) + perimeter);
areaMap.set(cell, (areaMap.get(cell) || 0) + 1);
const visited = new Set<string>();

function floodFill(start: Coord) {
const stack: Coord[] = [start];
const fieldId = grid.get(start.serialize());
let area = 0;
let perimeter = 0;

while (stack.length > 0) {
const current = stack.pop()!;
const currentStr = current.serialize();
if (visited.has(currentStr))
continue;

visited.add(currentStr);
area++;

for (const neighbor of getNeighborCoords(current)) {
if (grid.get(neighbor.serialize()) === fieldId) {
if (!visited.has(neighbor.serialize())) {
stack.push(neighbor);
}
} else {
perimeter++;
}
}
}
return { area, perimeter }
}

for (let [key, value] of perimeterMap) {
sum += value * areaMap.get(key)!;
let totalCost = 0;

for (const key of grid.keys()) {
if (!visited.has(key)) {
const { area, perimeter } = floodFill(Coord.deserialize(key))!;
totalCost += area * perimeter;
}
}

return sum;
return totalCost;
}
9 changes: 4 additions & 5 deletions 2024/src/2024/day12/testInput1
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
OOOOO
OXOXO
OOOOO
OXOXO
OOOOO
AAAA
BBCD
BBCC
EEEC
9 changes: 5 additions & 4 deletions 2024/src/2024/day12/testInput2
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
AAAA
BBCD
BBCC
EEEC
OOOOO
OXOXO
OOOOO
OXOXO
OOOOO

0 comments on commit 00a0ad4

Please sign in to comment.