mirror of
https://github.com/seigler/aoc2024
synced 2025-07-26 00:36:10 +00:00
day 12 part 2 fixed
This commit is contained in:
parent
86a1172282
commit
54414dce26
3 changed files with 118 additions and 101 deletions
|
@ -183,10 +183,10 @@
|
|||
"time": null
|
||||
},
|
||||
"part2": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "904114",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 220.713667
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
12
README.md
12
README.md
|
@ -22,7 +22,7 @@
|
|||
[](src/day09)
|
||||
[](src/day10)
|
||||
[](src/day11)
|
||||
[](src/day12)
|
||||
[](src/day12)
|
||||

|
||||

|
||||

|
||||
|
@ -146,9 +146,9 @@ Both parts: 130.69ms
|
|||
|
||||
```
|
||||
Day 12
|
||||
Time part 1: 197.261ms
|
||||
Time part 2: -
|
||||
Both parts: 197.261ms
|
||||
Time part 1: -
|
||||
Time part 2: 220.714ms
|
||||
Both parts: 220.714ms
|
||||
```
|
||||
|
||||
```
|
||||
|
@ -243,8 +243,8 @@ Both parts: -
|
|||
```
|
||||
|
||||
```
|
||||
Total stars: 23/50
|
||||
Total time: 38712.29ms
|
||||
Total stars: 24/50
|
||||
Total time: 38735.743ms
|
||||
```
|
||||
|
||||
<!--/RESULTS-->
|
||||
|
|
|
@ -1,19 +1,26 @@
|
|||
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
|
||||
[0, 1], // right
|
||||
[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) => {
|
||||
return directions.map(([dr, dc]) => [r + dr, c + dc])
|
||||
return orthogonals.map(([dr, dc]) => [r + dr, c + dc])
|
||||
}
|
||||
|
||||
const getKey = (items: unknown[]) => {
|
||||
return items.join(',')
|
||||
return items.join(",")
|
||||
}
|
||||
type Key = ReturnType<typeof getKey>
|
||||
|
||||
|
@ -34,30 +41,38 @@ const part1 = (rawInput: string) => {
|
|||
const neighbors = getNeighbors(r, c).map(getKey)
|
||||
let touches = 0
|
||||
let matchingRegions: Region[] = []
|
||||
regions.forEach(region => {
|
||||
regions.forEach((region) => {
|
||||
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) {
|
||||
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],
|
||||
})
|
||||
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, region) => total + region.perimeter * region.area, 0)
|
||||
return Array.from(regions).reduce(
|
||||
(total, region) => total + region.perimeter * region.area,
|
||||
0,
|
||||
)
|
||||
}
|
||||
|
||||
const part2 = (rawInput: string) => {
|
||||
|
@ -70,95 +85,97 @@ const part2 = (rawInput: string) => {
|
|||
const neighbors = getNeighbors(r, c).map(getKey)
|
||||
let touches = 0
|
||||
let matchingRegions: Region[] = []
|
||||
regions.forEach(region => {
|
||||
regions.forEach((region) => {
|
||||
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) {
|
||||
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],
|
||||
})
|
||||
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,
|
||||
}) => {
|
||||
return Array.from(regions).reduce((total, { symbol, plots, area }) => {
|
||||
let corners = 0
|
||||
plots.forEach(plot => {
|
||||
const [r, c] = plot.split(',').map(Number)
|
||||
const boundaries = getNeighbors(r, c).map(getKey).map((k, i) => plots.includes(k) ? '' : i).join('')
|
||||
switch(boundaries) {
|
||||
case '0123':
|
||||
corners += 4
|
||||
break
|
||||
case '01':
|
||||
case '12':
|
||||
case '23':
|
||||
case '03':
|
||||
plots.forEach((plot) => {
|
||||
const [r, c] = plot.split(",").map(Number)
|
||||
const orthogonalProbes = getNeighbors(r, c)
|
||||
.map(getKey)
|
||||
.map((k) => plots.includes(k))
|
||||
const diagonalProbes = diagonals.map(([dr, dc]) =>
|
||||
plots.includes(getKey([r + dr, c + dc])),
|
||||
)
|
||||
orthogonalProbes.forEach((presentThisDirection, thisDirection) => {
|
||||
const nextDirection = (thisDirection + 1) % 4
|
||||
const presentNextDirection = orthogonalProbes[nextDirection]
|
||||
if (
|
||||
presentThisDirection &&
|
||||
presentNextDirection &&
|
||||
!diagonalProbes[thisDirection]
|
||||
) {
|
||||
corners += 1
|
||||
break
|
||||
case '012':
|
||||
case '123':
|
||||
case '023':
|
||||
case '013':
|
||||
corners += 2
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!presentThisDirection && !presentNextDirection) {
|
||||
corners += 1
|
||||
}
|
||||
})
|
||||
})
|
||||
console.log(`Region of ${symbol} with ${corners} sides`)
|
||||
return total + corners * area
|
||||
}, 0)
|
||||
}
|
||||
|
||||
run({
|
||||
// part1: {
|
||||
// tests: [
|
||||
// {
|
||||
// input: `AAAA
|
||||
// BBCD
|
||||
// BBCC
|
||||
// EEEC`,
|
||||
// expected: 140,
|
||||
// },
|
||||
// {
|
||||
// input: `OOOOO
|
||||
// OXOXO
|
||||
// OOOOO
|
||||
// OXOXO
|
||||
// OOOOO`, expected: 772
|
||||
// },
|
||||
// {
|
||||
// input: `RRRRIICCFF
|
||||
// RRRRIICCCF
|
||||
// VVRRRCCFFF
|
||||
// VVRCCCJFFF
|
||||
// VVVVCJJCFE
|
||||
// VVIVCCJJEE
|
||||
// VVIIICJJEE
|
||||
// MIIIIIJJEE
|
||||
// MIIISIJEEE
|
||||
// MMMISSJEEE`,
|
||||
// expected: 1930
|
||||
// }
|
||||
// ],
|
||||
// solution: part1,
|
||||
// },
|
||||
// part1: {
|
||||
// tests: [
|
||||
// {
|
||||
// input: `AAAA
|
||||
// BBCD
|
||||
// BBCC
|
||||
// EEEC`,
|
||||
// expected: 140,
|
||||
// },
|
||||
// {
|
||||
// input: `OOOOO
|
||||
// OXOXO
|
||||
// OOOOO
|
||||
// OXOXO
|
||||
// OOOOO`, expected: 772
|
||||
// },
|
||||
// {
|
||||
// input: `RRRRIICCFF
|
||||
// RRRRIICCCF
|
||||
// VVRRRCCFFF
|
||||
// VVRCCCJFFF
|
||||
// VVVVCJJCFE
|
||||
// VVIVCCJJEE
|
||||
// VVIIICJJEE
|
||||
// MIIIIIJJEE
|
||||
// MIIISIJEEE
|
||||
// MMMISSJEEE`,
|
||||
// expected: 1930
|
||||
// }
|
||||
// ],
|
||||
// solution: part1,
|
||||
// },
|
||||
part2: {
|
||||
tests: [
|
||||
{
|
||||
|
@ -175,11 +192,11 @@ AAABBA
|
|||
ABBAAA
|
||||
ABBAAA
|
||||
AAAAAA`,
|
||||
expected: 1206,
|
||||
}
|
||||
expected: 368,
|
||||
},
|
||||
],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
onlyTests: true,
|
||||
onlyTests: false,
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue