advent of code 2023 day 16: naive recursive impl?
This commit is contained in:
parent
11d2c3bdf2
commit
ebda90a747
|
@ -1,6 +1,51 @@
|
|||
|
||||
const directions = {
|
||||
"U": [-1, 0],
|
||||
"D": [1, 0],
|
||||
"L": [0, -1],
|
||||
"R": [0, 1]
|
||||
}
|
||||
const mirrors = {
|
||||
"/": {
|
||||
"U": "R",
|
||||
"D": "L",
|
||||
"R": "U",
|
||||
"L": "D"
|
||||
},
|
||||
"\\": {
|
||||
"U": "L",
|
||||
"D": "R",
|
||||
"R": "U",
|
||||
"L": "D"
|
||||
}
|
||||
}
|
||||
|
||||
let i = 0
|
||||
const solve = (input) => {
|
||||
const tiles = input.split('\n').map(line => line.split(''))
|
||||
const energized = []
|
||||
|
||||
const traverse = (pos, direction) => {
|
||||
i++
|
||||
if(pos[0] < 0 || pos[1] < 0 || pos[0] > tiles.length || pos[1] > tiles[0].length || i > 50)
|
||||
return
|
||||
const curr = tiles[pos[0]][pos[1]]
|
||||
|
||||
energized.push(pos)
|
||||
if(curr === '/' || curr === '\\') {
|
||||
direction = mirrors[curr][direction]
|
||||
} else if(curr === '|' && (direction === "L" || direction === "R")) {
|
||||
traverse([pos[0] + directions["U"][0], pos[1] + directions["U"][1]], "U")
|
||||
traverse([pos[0] + directions["D"][0], pos[1] + directions["D"][1]], "D")
|
||||
} else if(curr === '-' && (direction === "U" || direction === "D")) {
|
||||
// I think the stop condition for the recursion is wonky? Doing R first creates a stack overflow.
|
||||
traverse([pos[0] + directions["L"][0], pos[1] + directions["L"][1]], "L")
|
||||
traverse([pos[0] + directions["R"][0], pos[1] + directions["R"][1]], "R")
|
||||
}
|
||||
traverse([pos[0] + directions[direction][0], pos[1] + directions[direction][1]], direction)
|
||||
}
|
||||
traverse([0, 0], "R")
|
||||
return new Set(energized).size
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
Loading…
Reference in New Issue