day 12 part 2 fixed

This commit is contained in:
Joshua Seigler 2024-12-12 14:13:38 -05:00
parent 86a1172282
commit 54414dce26
No known key found for this signature in database
3 changed files with 118 additions and 101 deletions

View file

@ -183,10 +183,10 @@
"time": null "time": null
}, },
"part2": { "part2": {
"solved": false, "solved": true,
"result": null, "result": "904114",
"attempts": [], "attempts": [],
"time": null "time": 220.713667
} }
}, },
{ {

View file

@ -22,7 +22,7 @@
[![Day](https://badgen.net/badge/09/%E2%98%85%E2%98%85/green)](src/day09) [![Day](https://badgen.net/badge/09/%E2%98%85%E2%98%85/green)](src/day09)
[![Day](https://badgen.net/badge/10/%E2%98%85%E2%98%85/green)](src/day10) [![Day](https://badgen.net/badge/10/%E2%98%85%E2%98%85/green)](src/day10)
[![Day](https://badgen.net/badge/11/%E2%98%85%E2%98%85/green)](src/day11) [![Day](https://badgen.net/badge/11/%E2%98%85%E2%98%85/green)](src/day11)
[![Day](https://badgen.net/badge/12/%E2%98%85%E2%98%86/yellow)](src/day12) [![Day](https://badgen.net/badge/12/%E2%98%85%E2%98%85/green)](src/day12)
![Day](https://badgen.net/badge/13/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/13/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/14/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/14/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/15/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/15/%E2%98%86%E2%98%86/gray)
@ -146,9 +146,9 @@ Both parts: 130.69ms
``` ```
Day 12 Day 12
Time part 1: 197.261ms Time part 1: -
Time part 2: - Time part 2: 220.714ms
Both parts: 197.261ms Both parts: 220.714ms
``` ```
``` ```
@ -243,8 +243,8 @@ Both parts: -
``` ```
``` ```
Total stars: 23/50 Total stars: 24/50
Total time: 38712.29ms Total time: 38735.743ms
``` ```
<!--/RESULTS--> <!--/RESULTS-->

View file

@ -1,19 +1,26 @@
import run from "aocrunner" import run from "aocrunner"
const parseInput = (rawInput: string) => rawInput.split('\n').map(line => line.split('')) const parseInput = (rawInput: string) =>
rawInput.split("\n").map((line) => line.split(""))
const directions = [ const orthogonals = [
[-1, 0], // up [-1, 0], // up
[0, 1], // right [0, 1], // right
[1, 0], // down [1, 0], // down
[0, -1] // left [0, -1], // left
]
const diagonals = [
[-1, 1], // up right
[1, 1], // down right
[1, -1], // down left
[-1, -1], // up left
] ]
const getNeighbors = (r: number, c: number) => { const getNeighbors = (r: number, c: number) => {
return directions.map(([dr, dc]) => [r + dr, c + dc]) return orthogonals.map(([dr, dc]) => [r + dr, c + dc])
} }
const getKey = (items: unknown[]) => { const getKey = (items: unknown[]) => {
return items.join(',') return items.join(",")
} }
type Key = ReturnType<typeof getKey> type Key = ReturnType<typeof getKey>
@ -34,30 +41,38 @@ const part1 = (rawInput: string) => {
const neighbors = getNeighbors(r, c).map(getKey) const neighbors = getNeighbors(r, c).map(getKey)
let touches = 0 let touches = 0
let matchingRegions: Region[] = [] let matchingRegions: Region[] = []
regions.forEach(region => { regions.forEach((region) => {
if (region.symbol !== symbol) return if (region.symbol !== symbol) return
const newTouches = region.plots.filter(c => neighbors.includes(c)).length const newTouches = region.plots.filter((c) =>
neighbors.includes(c),
).length
if (newTouches > 0) { if (newTouches > 0) {
touches += newTouches touches += newTouches
matchingRegions.push(region) matchingRegions.push(region)
regions.delete(region) regions.delete(region)
} }
}) })
const newRegion = matchingRegions.reduce<Region>((acc, cur) => { const newRegion = matchingRegions.reduce<Region>(
(acc, cur) => {
acc.area += cur.area acc.area += cur.area
acc.perimeter += cur.perimeter acc.perimeter += cur.perimeter
acc.plots.push(...cur.plots) acc.plots.push(...cur.plots)
return acc return acc
}, { },
{
symbol, symbol,
area: 1, area: 1,
perimeter: 4 - 2 * touches, perimeter: 4 - 2 * touches,
plots: [key], plots: [key],
}) },
)
regions.add(newRegion) regions.add(newRegion)
}) })
}) })
return Array.from(regions).reduce((total, region) => total + region.perimeter * region.area, 0) return Array.from(regions).reduce(
(total, region) => total + region.perimeter * region.area,
0,
)
} }
const part2 = (rawInput: string) => { const part2 = (rawInput: string) => {
@ -70,58 +85,60 @@ const part2 = (rawInput: string) => {
const neighbors = getNeighbors(r, c).map(getKey) const neighbors = getNeighbors(r, c).map(getKey)
let touches = 0 let touches = 0
let matchingRegions: Region[] = [] let matchingRegions: Region[] = []
regions.forEach(region => { regions.forEach((region) => {
if (region.symbol !== symbol) return if (region.symbol !== symbol) return
const newTouches = region.plots.filter(c => neighbors.includes(c)).length const newTouches = region.plots.filter((c) =>
neighbors.includes(c),
).length
if (newTouches > 0) { if (newTouches > 0) {
touches += newTouches touches += newTouches
matchingRegions.push(region) matchingRegions.push(region)
regions.delete(region) regions.delete(region)
} }
}) })
const newRegion = matchingRegions.reduce<Region>((acc, cur) => { const newRegion = matchingRegions.reduce<Region>(
(acc, cur) => {
acc.area += cur.area acc.area += cur.area
acc.perimeter += cur.perimeter acc.perimeter += cur.perimeter
acc.plots.push(...cur.plots) acc.plots.push(...cur.plots)
return acc return acc
}, { },
{
symbol, symbol,
area: 1, area: 1,
perimeter: 4 - 2 * touches, perimeter: 4 - 2 * touches,
plots: [key], plots: [key],
}) },
)
regions.add(newRegion) regions.add(newRegion)
}) })
}) })
return Array.from(regions).reduce((total, { return Array.from(regions).reduce((total, { symbol, plots, area }) => {
symbol,
plots,
area,
}) => {
let corners = 0 let corners = 0
plots.forEach(plot => { plots.forEach((plot) => {
const [r, c] = plot.split(',').map(Number) const [r, c] = plot.split(",").map(Number)
const boundaries = getNeighbors(r, c).map(getKey).map((k, i) => plots.includes(k) ? '' : i).join('') const orthogonalProbes = getNeighbors(r, c)
switch(boundaries) { .map(getKey)
case '0123': .map((k) => plots.includes(k))
corners += 4 const diagonalProbes = diagonals.map(([dr, dc]) =>
break plots.includes(getKey([r + dr, c + dc])),
case '01': )
case '12': orthogonalProbes.forEach((presentThisDirection, thisDirection) => {
case '23': const nextDirection = (thisDirection + 1) % 4
case '03': const presentNextDirection = orthogonalProbes[nextDirection]
if (
presentThisDirection &&
presentNextDirection &&
!diagonalProbes[thisDirection]
) {
corners += 1
}
if (!presentThisDirection && !presentNextDirection) {
corners += 1 corners += 1
break
case '012':
case '123':
case '023':
case '013':
corners += 2
break
} }
}) })
console.log(`Region of ${symbol} with ${corners} sides`) })
return total + corners * area return total + corners * area
}, 0) }, 0)
} }
@ -175,11 +192,11 @@ AAABBA
ABBAAA ABBAAA
ABBAAA ABBAAA
AAAAAA`, AAAAAA`,
expected: 1206, expected: 368,
} },
], ],
solution: part2, solution: part2,
}, },
trimTestInputs: true, trimTestInputs: true,
onlyTests: true, onlyTests: false,
}) })