From e1745ac0c4fe028db0e322708f83a605a12a2548 Mon Sep 17 00:00:00 2001 From: martapanc Date: Thu, 12 Dec 2024 09:39:54 +0100 Subject: [PATCH] 2024D12: Part 1 wip --- 2024/src/2024/day12/day12.test.ts | 15 ++++++++++++ 2024/src/2024/day12/day12.ts | 38 +++++++++++++++++++++++++++++++ 2024/src/2024/day12/testInput1 | 5 ++++ 2024/src/2024/day12/testInput2 | 4 ++++ 2024/src/2024/day12/testInput3 | 10 ++++++++ 5 files changed, 72 insertions(+) create mode 100644 2024/src/2024/day12/day12.test.ts create mode 100644 2024/src/2024/day12/day12.ts create mode 100644 2024/src/2024/day12/testInput1 create mode 100644 2024/src/2024/day12/testInput2 create mode 100644 2024/src/2024/day12/testInput3 diff --git a/2024/src/2024/day12/day12.test.ts b/2024/src/2024/day12/day12.test.ts new file mode 100644 index 0000000..b6fe845 --- /dev/null +++ b/2024/src/2024/day12/day12.test.ts @@ -0,0 +1,15 @@ +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); + }); + + test('Part 2', async () => { + expect(await part2('testInput1')).toEqual(31); + expect(await part2('input')).toEqual(29379307); + }); +}); \ No newline at end of file diff --git a/2024/src/2024/day12/day12.ts b/2024/src/2024/day12/day12.ts new file mode 100644 index 0000000..91c3d61 --- /dev/null +++ b/2024/src/2024/day12/day12.ts @@ -0,0 +1,38 @@ +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"; + +export async function part1(inputFile: string) { + return await day12(inputFile, calcAreaAndPerimeter); +} + +export async function part2(inputFile: string) { + return await day12(inputFile); +} + +async function day12(inputFile: string, calcFn?: (grid: Grid) => number) { + const inputPath = path.join(__dirname, inputFile); + const lines = await readInputLineByLine(inputPath); + + return calcFn?.(readLinesToGrid(lines)); +} + +function calcAreaAndPerimeter(grid: Grid) { + let sum = 0; + const perimeterMap: Map = new Map(); + const areaMap: Map = 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); + } + + for (let [key, value] of perimeterMap) { + sum += value * areaMap.get(key)!; + } + + return sum; +} \ No newline at end of file diff --git a/2024/src/2024/day12/testInput1 b/2024/src/2024/day12/testInput1 new file mode 100644 index 0000000..cc213c5 --- /dev/null +++ b/2024/src/2024/day12/testInput1 @@ -0,0 +1,5 @@ +OOOOO +OXOXO +OOOOO +OXOXO +OOOOO \ No newline at end of file diff --git a/2024/src/2024/day12/testInput2 b/2024/src/2024/day12/testInput2 new file mode 100644 index 0000000..cc5f968 --- /dev/null +++ b/2024/src/2024/day12/testInput2 @@ -0,0 +1,4 @@ +AAAA +BBCD +BBCC +EEEC \ No newline at end of file diff --git a/2024/src/2024/day12/testInput3 b/2024/src/2024/day12/testInput3 new file mode 100644 index 0000000..85b768f --- /dev/null +++ b/2024/src/2024/day12/testInput3 @@ -0,0 +1,10 @@ +RRRRIICCFF +RRRRIICCCF +VVRRRCCFFF +VVRCCCJFFF +VVVVCJJCFE +VVIVCCJJEE +VVIIICJJEE +MIIIIIJJEE +MIIISIJEEE +MMMISSJEEE