mirror of
https://github.com/seigler/aoc2024
synced 2025-07-27 00:56:10 +00:00
day 12 tidied
This commit is contained in:
parent
54414dce26
commit
6490326b05
2 changed files with 54 additions and 77 deletions
|
@ -176,17 +176,17 @@
|
||||||
{
|
{
|
||||||
"part1": {
|
"part1": {
|
||||||
"solved": true,
|
"solved": true,
|
||||||
"result": null,
|
"result": "1461752",
|
||||||
"attempts": [
|
"attempts": [
|
||||||
"813344"
|
"813344"
|
||||||
],
|
],
|
||||||
"time": null
|
"time": 172.252042
|
||||||
},
|
},
|
||||||
"part2": {
|
"part2": {
|
||||||
"solved": true,
|
"solved": true,
|
||||||
"result": "904114",
|
"result": "904114",
|
||||||
"attempts": [],
|
"attempts": [],
|
||||||
"time": 220.713667
|
"time": 214.983875
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -9,12 +9,14 @@ const orthogonals = [
|
||||||
[1, 0], // down
|
[1, 0], // down
|
||||||
[0, -1], // left
|
[0, -1], // left
|
||||||
]
|
]
|
||||||
|
|
||||||
const diagonals = [
|
const diagonals = [
|
||||||
[-1, 1], // up right
|
[-1, 1], // up right
|
||||||
[1, 1], // down right
|
[1, 1], // down right
|
||||||
[1, -1], // down left
|
[1, -1], // down left
|
||||||
[-1, -1], // up left
|
[-1, -1], // up left
|
||||||
]
|
]
|
||||||
|
|
||||||
const getNeighbors = (r: number, c: number) => {
|
const getNeighbors = (r: number, c: number) => {
|
||||||
return orthogonals.map(([dr, dc]) => [r + dr, c + dc])
|
return orthogonals.map(([dr, dc]) => [r + dr, c + dc])
|
||||||
}
|
}
|
||||||
|
@ -22,6 +24,11 @@ const getNeighbors = (r: number, c: number) => {
|
||||||
const getKey = (items: unknown[]) => {
|
const getKey = (items: unknown[]) => {
|
||||||
return items.join(",")
|
return items.join(",")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const unpackKey = <T>(key: Key, mapper: (input: string) => T) => {
|
||||||
|
return key.split(",").map(mapper)
|
||||||
|
}
|
||||||
|
|
||||||
type Key = ReturnType<typeof getKey>
|
type Key = ReturnType<typeof getKey>
|
||||||
|
|
||||||
type Region = {
|
type Region = {
|
||||||
|
@ -31,11 +38,10 @@ type Region = {
|
||||||
plots: Key[]
|
plots: Key[]
|
||||||
}
|
}
|
||||||
|
|
||||||
const part1 = (rawInput: string) => {
|
const getRegions = (map: string[][]) => {
|
||||||
const input = parseInput(rawInput)
|
|
||||||
let regions = new Set<Region>()
|
let regions = new Set<Region>()
|
||||||
|
|
||||||
input.forEach((row, r) => {
|
map.forEach((row, r) => {
|
||||||
row.forEach((symbol, c) => {
|
row.forEach((symbol, c) => {
|
||||||
const key = getKey([r, c])
|
const key = getKey([r, c])
|
||||||
const neighbors = getNeighbors(r, c).map(getKey)
|
const neighbors = getNeighbors(r, c).map(getKey)
|
||||||
|
@ -69,7 +75,12 @@ const part1 = (rawInput: string) => {
|
||||||
regions.add(newRegion)
|
regions.add(newRegion)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
return Array.from(regions).reduce(
|
return regions
|
||||||
|
}
|
||||||
|
|
||||||
|
const part1 = (rawInput: string) => {
|
||||||
|
const input = parseInput(rawInput)
|
||||||
|
return Array.from(getRegions(input)).reduce(
|
||||||
(total, region) => total + region.perimeter * region.area,
|
(total, region) => total + region.perimeter * region.area,
|
||||||
0,
|
0,
|
||||||
)
|
)
|
||||||
|
@ -77,47 +88,12 @@ const part1 = (rawInput: string) => {
|
||||||
|
|
||||||
const part2 = (rawInput: string) => {
|
const part2 = (rawInput: string) => {
|
||||||
const input = parseInput(rawInput)
|
const input = parseInput(rawInput)
|
||||||
let regions = new Set<Region>()
|
const regions = getRegions(input)
|
||||||
|
|
||||||
input.forEach((row, r) => {
|
return Array.from(regions).reduce((total, { plots, area }) => {
|
||||||
row.forEach((symbol, c) => {
|
|
||||||
const key = getKey([r, c])
|
|
||||||
const neighbors = getNeighbors(r, c).map(getKey)
|
|
||||||
let touches = 0
|
|
||||||
let matchingRegions: Region[] = []
|
|
||||||
regions.forEach((region) => {
|
|
||||||
if (region.symbol !== symbol) return
|
|
||||||
const newTouches = region.plots.filter((c) =>
|
|
||||||
neighbors.includes(c),
|
|
||||||
).length
|
|
||||||
if (newTouches > 0) {
|
|
||||||
touches += newTouches
|
|
||||||
matchingRegions.push(region)
|
|
||||||
regions.delete(region)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
const newRegion = matchingRegions.reduce<Region>(
|
|
||||||
(acc, cur) => {
|
|
||||||
acc.area += cur.area
|
|
||||||
acc.perimeter += cur.perimeter
|
|
||||||
acc.plots.push(...cur.plots)
|
|
||||||
return acc
|
|
||||||
},
|
|
||||||
{
|
|
||||||
symbol,
|
|
||||||
area: 1,
|
|
||||||
perimeter: 4 - 2 * touches,
|
|
||||||
plots: [key],
|
|
||||||
},
|
|
||||||
)
|
|
||||||
regions.add(newRegion)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
return Array.from(regions).reduce((total, { 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] = unpackKey(plot, Number)
|
||||||
const orthogonalProbes = getNeighbors(r, c)
|
const orthogonalProbes = getNeighbors(r, c)
|
||||||
.map(getKey)
|
.map(getKey)
|
||||||
.map((k) => plots.includes(k))
|
.map((k) => plots.includes(k))
|
||||||
|
@ -144,38 +120,39 @@ const part2 = (rawInput: string) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
run({
|
run({
|
||||||
// part1: {
|
part1: {
|
||||||
// tests: [
|
tests: [
|
||||||
// {
|
{
|
||||||
// input: `AAAA
|
input: `AAAA
|
||||||
// BBCD
|
BBCD
|
||||||
// BBCC
|
BBCC
|
||||||
// EEEC`,
|
EEEC`,
|
||||||
// expected: 140,
|
expected: 140,
|
||||||
// },
|
},
|
||||||
// {
|
{
|
||||||
// input: `OOOOO
|
input: `OOOOO
|
||||||
// OXOXO
|
OXOXO
|
||||||
// OOOOO
|
OOOOO
|
||||||
// OXOXO
|
OXOXO
|
||||||
// OOOOO`, expected: 772
|
OOOOO`,
|
||||||
// },
|
expected: 772,
|
||||||
// {
|
},
|
||||||
// input: `RRRRIICCFF
|
{
|
||||||
// RRRRIICCCF
|
input: `RRRRIICCFF
|
||||||
// VVRRRCCFFF
|
RRRRIICCCF
|
||||||
// VVRCCCJFFF
|
VVRRRCCFFF
|
||||||
// VVVVCJJCFE
|
VVRCCCJFFF
|
||||||
// VVIVCCJJEE
|
VVVVCJJCFE
|
||||||
// VVIIICJJEE
|
VVIVCCJJEE
|
||||||
// MIIIIIJJEE
|
VVIIICJJEE
|
||||||
// MIIISIJEEE
|
MIIIIIJJEE
|
||||||
// MMMISSJEEE`,
|
MIIISIJEEE
|
||||||
// expected: 1930
|
MMMISSJEEE`,
|
||||||
// }
|
expected: 1930,
|
||||||
// ],
|
},
|
||||||
// solution: part1,
|
],
|
||||||
// },
|
solution: part1,
|
||||||
|
},
|
||||||
part2: {
|
part2: {
|
||||||
tests: [
|
tests: [
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue