mirror of
https://github.com/seigler/aoc2024
synced 2025-07-26 00:36:10 +00:00
day 12, part 1 only
This commit is contained in:
parent
52437aeb8a
commit
86a1172282
4 changed files with 206 additions and 10 deletions
|
@ -175,9 +175,11 @@
|
|||
},
|
||||
{
|
||||
"part1": {
|
||||
"solved": false,
|
||||
"solved": true,
|
||||
"result": null,
|
||||
"attempts": [],
|
||||
"attempts": [
|
||||
"813344"
|
||||
],
|
||||
"time": null
|
||||
},
|
||||
"part2": {
|
||||
|
|
16
README.md
16
README.md
|
@ -22,7 +22,7 @@
|
|||
[](src/day09)
|
||||
[](src/day10)
|
||||
[](src/day11)
|
||||

|
||||
[](src/day12)
|
||||

|
||||

|
||||

|
||||
|
@ -139,16 +139,16 @@ Both parts: 11.016ms
|
|||
|
||||
```
|
||||
Day 11
|
||||
Time part 1: 54.46ms
|
||||
Time part 2: 130.251ms
|
||||
Both parts: 184.711ms
|
||||
Time part 1: 4.472ms
|
||||
Time part 2: 126.218ms
|
||||
Both parts: 130.69ms
|
||||
```
|
||||
|
||||
```
|
||||
Day 12
|
||||
Time part 1: -
|
||||
Time part 1: 197.261ms
|
||||
Time part 2: -
|
||||
Both parts: -
|
||||
Both parts: 197.261ms
|
||||
```
|
||||
|
||||
```
|
||||
|
@ -243,8 +243,8 @@ Both parts: -
|
|||
```
|
||||
|
||||
```
|
||||
Total stars: 22/50
|
||||
Total time: 38569.05ms
|
||||
Total stars: 23/50
|
||||
Total time: 38712.29ms
|
||||
```
|
||||
|
||||
<!--/RESULTS-->
|
||||
|
|
9
src/day12/README.md
Normal file
9
src/day12/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2024 - day 12 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2024/day/12)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
185
src/day12/index.ts
Normal file
185
src/day12/index.ts
Normal file
|
@ -0,0 +1,185 @@
|
|||
import run from "aocrunner"
|
||||
|
||||
const parseInput = (rawInput: string) => rawInput.split('\n').map(line => line.split(''))
|
||||
|
||||
const directions = [
|
||||
[-1, 0], // up
|
||||
[0, 1], // right
|
||||
[1, 0], // down
|
||||
[0, -1] // left
|
||||
]
|
||||
const getNeighbors = (r: number, c: number) => {
|
||||
return directions.map(([dr, dc]) => [r + dr, c + dc])
|
||||
}
|
||||
|
||||
const getKey = (items: unknown[]) => {
|
||||
return items.join(',')
|
||||
}
|
||||
type Key = ReturnType<typeof getKey>
|
||||
|
||||
type Region = {
|
||||
symbol: string
|
||||
area: number
|
||||
perimeter: number
|
||||
plots: Key[]
|
||||
}
|
||||
|
||||
const part1 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
let regions = new Set<Region>()
|
||||
|
||||
input.forEach((row, r) => {
|
||||
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, region) => total + region.perimeter * region.area, 0)
|
||||
}
|
||||
|
||||
const part2 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
let regions = new Set<Region>()
|
||||
|
||||
input.forEach((row, r) => {
|
||||
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
|
||||
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':
|
||||
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
|
||||
}, 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,
|
||||
// },
|
||||
part2: {
|
||||
tests: [
|
||||
{
|
||||
input: `AAAA
|
||||
BBCD
|
||||
BBCC
|
||||
EEEC`,
|
||||
expected: 80,
|
||||
},
|
||||
{
|
||||
input: `AAAAAA
|
||||
AAABBA
|
||||
AAABBA
|
||||
ABBAAA
|
||||
ABBAAA
|
||||
AAAAAA`,
|
||||
expected: 1206,
|
||||
}
|
||||
],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
onlyTests: true,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue