mirror of
https://github.com/seigler/aoc2024
synced 2025-07-26 00:36:10 +00:00
much cleaner way to look for shapes in grids
This commit is contained in:
parent
54dc419f8a
commit
5764869e99
2 changed files with 28 additions and 104 deletions
|
@ -48,17 +48,17 @@
|
|||
{
|
||||
"part1": {
|
||||
"solved": true,
|
||||
"result": "2358",
|
||||
"result": "2143",
|
||||
"attempts": [
|
||||
"0"
|
||||
],
|
||||
"time": 327.70272
|
||||
"time": 67.709144
|
||||
},
|
||||
"part2": {
|
||||
"solved": true,
|
||||
"result": "1737",
|
||||
"attempts": [],
|
||||
"time": 201.22507
|
||||
"time": 182.986319
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
@ -4,118 +4,42 @@ const parseInput = (rawInput: string) => {
|
|||
return rawInput.split('\n').map(line => line.split(''))
|
||||
}
|
||||
|
||||
const directions = ["down","right","dl","dr"] as const
|
||||
type Direction = typeof directions[number]
|
||||
type Prospect1 = {
|
||||
letters: string
|
||||
direction: Direction
|
||||
next: [number, number]
|
||||
}
|
||||
type Coord = Prospect1['next']
|
||||
const getNext = (dir: Direction, [row, col]: Coord): Coord => {
|
||||
switch(dir) {
|
||||
case "down": {return [row+1, col]}
|
||||
case "right": {return [row, col+1]}
|
||||
case "dl": {return [row+1, col-1]}
|
||||
case "dr": {return [row+1, col+1]}
|
||||
}
|
||||
}
|
||||
|
||||
const part1 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
let total = 0
|
||||
let prospects: Prospect1[] = []
|
||||
input.forEach((line, row) => {
|
||||
line.forEach((letter, col) => {
|
||||
prospects = prospects.map((prospect) => {
|
||||
const {
|
||||
direction,
|
||||
letters,
|
||||
next
|
||||
} = prospect
|
||||
if (next[0] < row) return null
|
||||
if (next[0] === row && next[1] === col) {
|
||||
const nextLetters = letters + letter
|
||||
if (["XMAS","SAMX"].includes(nextLetters)) {
|
||||
total += 1
|
||||
return null
|
||||
}
|
||||
if (["XMA","SAM","XM","SA"].includes(nextLetters)) {
|
||||
return {
|
||||
letters: nextLetters,
|
||||
direction: direction,
|
||||
next: getNext(direction, next)
|
||||
}
|
||||
}
|
||||
}
|
||||
return prospect
|
||||
}).filter(prospect => prospect != null)
|
||||
if ("XMAS".includes(letter)) {
|
||||
directions.forEach(direction => {
|
||||
prospects.push({
|
||||
letters: letter,
|
||||
direction,
|
||||
next: getNext(direction, [row, col])
|
||||
})
|
||||
})
|
||||
}
|
||||
input.forEach((line,r) => {
|
||||
line.forEach((cell, c) => {
|
||||
total += [
|
||||
[[r,c],[r,c+1],[r,c+2],[r,c+3]], // right
|
||||
[[r,c],[r+1,c+1],[r+2,c+2],[r+3,c+3]], // downright
|
||||
[[r,c],[r+1,c],[r+2,c],[r+3,c]], // down
|
||||
[[r,c],[r+1,c-1],[r+2,c-2],[r+3,c-3]], // downleft
|
||||
].reduce((acc,cur) => /XMAS|SAMX/.test(cur.map(([r,c]) => {
|
||||
return (input[r] ?? [])[c] ?? "."
|
||||
}).join('')) ? acc+1:acc, 0)
|
||||
})
|
||||
})
|
||||
return total
|
||||
}
|
||||
|
||||
type Prospect2 = {
|
||||
letters: string
|
||||
direction: Direction
|
||||
next: [number, number]
|
||||
center: [number, number]
|
||||
}
|
||||
|
||||
const part2 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
let centers = new Map<string, number>()
|
||||
let prospects: Prospect2[] = []
|
||||
let total = 0
|
||||
input.forEach((line, row) => {
|
||||
line.forEach((letter, col) => {
|
||||
prospects = prospects.map((prospect) => {
|
||||
const {
|
||||
direction,
|
||||
letters,
|
||||
next
|
||||
} = prospect
|
||||
if (next[0] < row) return null
|
||||
if (next[0] === row && next[1] === col) {
|
||||
const nextLetters = letters + letter
|
||||
if (["MAS","SAM"].includes(nextLetters)) {
|
||||
const key = prospect.center.join(",")
|
||||
if (centers.has(key)) { total++ }
|
||||
centers.set(key, 1)
|
||||
return null
|
||||
}
|
||||
if (["MA","SA"].includes(nextLetters)) {
|
||||
return {
|
||||
...prospect,
|
||||
letters: nextLetters,
|
||||
next: getNext(direction, next),
|
||||
}
|
||||
}
|
||||
}
|
||||
return prospect
|
||||
}).filter(prospect => prospect != null)
|
||||
if ("XMAS".includes(letter)) {
|
||||
["dl" as const, "dr" as const].forEach(direction => {
|
||||
const center = getNext(direction, [row, col])
|
||||
prospects.push({
|
||||
letters: letter,
|
||||
direction,
|
||||
next: center,
|
||||
center,
|
||||
})
|
||||
})
|
||||
for (let r = 0; r < input.length -2; r++) {
|
||||
for (let c = 0; c < input[0].length -2; c++) {
|
||||
if(/MMASS|SSAMM|MSAMS|SMASM/.test(
|
||||
[
|
||||
[r,c],
|
||||
[r,c+2],
|
||||
[r+1,c+1],
|
||||
[r+2,c],
|
||||
[r+2,c+2]
|
||||
].map(([r,c]) => input[r][c]).join('')
|
||||
)) {
|
||||
total++
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
|
@ -165,5 +89,5 @@ MXMXAXMASX`,
|
|||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
onlyTests: false,
|
||||
onlyTests: true,
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue