day 15, with console animations

This commit is contained in:
Joshua Seigler 2024-12-15 19:22:26 -05:00
parent 57455999f4
commit 4ba525c65f
2 changed files with 33 additions and 23 deletions

View file

@ -226,13 +226,13 @@
"solved": true, "solved": true,
"result": "1446158", "result": "1446158",
"attempts": [], "attempts": [],
"time": 4.47472 "time": 36404.188795
}, },
"part2": { "part2": {
"solved": true, "solved": true,
"result": "1446175", "result": "1446175",
"attempts": [], "attempts": [],
"time": 7.353299 "time": 75398.808507
} }
}, },
{ {

View file

@ -1,4 +1,5 @@
import run from "aocrunner" import run from "aocrunner"
import * as readline from "readline"
type Coord = [number, number] type Coord = [number, number]
@ -39,7 +40,7 @@ const part1 = (rawInput: string) => {
// Solving // Solving
let [rPos, cPos] = start let [rPos, cPos] = start
movements.forEach((move) => { movements.forEach((move, moveNum) => {
const [dr, dc] = deltas[move] const [dr, dc] = deltas[move]
for (let i = 1; ; i++) { for (let i = 1; ; i++) {
const key = getKey(rPos + dr * i, cPos + dc * i) const key = getKey(rPos + dr * i, cPos + dc * i)
@ -58,19 +59,24 @@ const part1 = (rawInput: string) => {
// Uncomment to show moves // Uncomment to show moves
// if (moveNum > 0) {
// readline.moveCursor(process.stdout, 0, - map.length - 1)
// }
// console.log(move) // console.log(move)
// const canvas: string[] = []
// map.forEach((row,r) => { // map.forEach((row,r) => {
// const line = row.map((_, c) => { // const line = row.map((_, c) => {
// const key = getKey(r,c) // const key = getKey(r,c)
// if (boxes.has(key)) return "O" // if (boxes.has(key)) return "\x1b[33;1mO\x1b[0m"
// if (walls.has(key)) return "#" // if (walls.has(key)) return "\x1b[30;100m \x1b[0m"
// if (rPos === r && cPos === c) return "@" // if (rPos === r && cPos === c) return "\x1b[96m@\x1b[0m"
// return " " // return " "
// }).join('') // }).join('')
// console.log(line) // canvas.push(line)
// }) // })
// console.log('') // console.log(canvas.join('\n'))
}) })
return Array.from(boxes.values()).reduce((total, b) => total + b, 0) return Array.from(boxes.values()).reduce((total, b) => total + b, 0)
} }
@ -101,8 +107,8 @@ const part2 = (rawInput: string) => {
// Solving // Solving
let [rPos, cPos] = start let [rPos, cPos] = start
movements.forEach((instruction) => { movements.forEach((move, moveNum) => {
const [dr, dc] = deltas[instruction] const [dr, dc] = deltas[move]
if (dr === 0) { // horizontal movement if (dr === 0) { // horizontal movement
for (let i = 1; ; i++) { for (let i = 1; ; i++) {
const key = getKey(rPos, cPos + dc * i) const key = getKey(rPos, cPos + dc * i)
@ -132,11 +138,11 @@ const part2 = (rawInput: string) => {
const toDelete: Key[] = [] const toDelete: Key[] = []
const toAdd: Key[] = [] const toAdd: Key[] = []
// solve this with recursion! // solve this with recursion!
const move = (r: number, c: number, dr: number): boolean => { const push = (r: number, c: number, dr: number): boolean => {
const keyAhead = getKey(r + dr, c) const keyAhead = getKey(r + dr, c)
if (walls.has(keyAhead)) return false if (walls.has(keyAhead)) return false
if (boxes.has(keyAhead)) { if (boxes.has(keyAhead)) {
if (move(r + dr, c, dr) && move(r + dr, c + 1, dr)) { if (push(r + dr, c, dr) && push(r + dr, c + 1, dr)) {
toDelete.push(keyAhead) toDelete.push(keyAhead)
toAdd.push(getKey(r+2*dr, c)) toAdd.push(getKey(r+2*dr, c))
return true return true
@ -145,7 +151,7 @@ const part2 = (rawInput: string) => {
} }
const keyLeftAhead = getKey(r + dr, c-1) const keyLeftAhead = getKey(r + dr, c-1)
if (boxes.has(keyLeftAhead)) { if (boxes.has(keyLeftAhead)) {
if (move(r + dr, c - 1, dr) && move(r + dr, c, dr)) { if (push(r + dr, c - 1, dr) && push(r + dr, c, dr)) {
toDelete.push(keyLeftAhead) toDelete.push(keyLeftAhead)
toAdd.push(getKey(r + 2*dr, c - 1)) toAdd.push(getKey(r + 2*dr, c - 1))
return true return true
@ -154,7 +160,7 @@ const part2 = (rawInput: string) => {
} }
return true return true
} }
if (move(rPos, cPos, dr)) { if (push(rPos, cPos, dr)) {
toDelete.forEach(k => boxes.delete(k)) toDelete.forEach(k => boxes.delete(k))
toAdd.forEach(k => boxes.add(k)) toAdd.forEach(k => boxes.add(k))
} else { } else {
@ -166,19 +172,23 @@ const part2 = (rawInput: string) => {
// Uncomment to show moves // Uncomment to show moves
// console.log(instruction) // if (moveNum > 0) {
// readline.moveCursor(process.stdout, 0, - map.length - 1)
// }
// console.log(move)
// const canvas: string[] = []
// map.forEach((row,r) => { // map.forEach((row,r) => {
// const line = new Array(row.length * 2).fill(0).map((_, c) => { // const line = new Array(row.length * 2).fill(0).map((_, c) => {
// const key = getKey(r,c) // const key = getKey(r,c)
// if (rPos === r && cPos === c) return "@" // if (rPos === r && cPos === c) return "\x1b[96m@\x1b[0m"
// if (boxes.has(key)) return "[" // if (boxes.has(key)) return "\x1b[33;1m[\x1b[0m"
// if (boxes.has(getKey(r, c-1))) return "]" // if (boxes.has(getKey(r, c-1))) return "\x1b[33;1m]\x1b[0m"
// if (walls.has(key)) return "#" // if (walls.has(key)) return "\x1b[30;100m \x1b[0m"
// return "." // return " "
// }).join('') // }).join('')
// console.log(line) // canvas.push(line)
// }) // })
// console.log('') // console.log(canvas.join('\n'))
}) })
return Array.from(boxes.values()).reduce((total, b) => total + b, 0) return Array.from(boxes.values()).reduce((total, b) => total + b, 0)
} }