This commit is contained in:
nim-ka
2022-11-25 08:39:30 +00:00
parent 48f56e1172
commit 91cf70e1f7
4 changed files with 74 additions and 2 deletions

36
2021/24.js Normal file
View File

@@ -0,0 +1,36 @@
function day24(input, part2) {
let lines = input.split("\n")
let ops = lines.splitOnElement("inp w").filter((e) => e.length).transpose()
let compareVals = ops[4].map((e) => +e.split(" ").last)
let stackVals = ops[14].map((e) => +e.split(" ").last)
let len = compareVals.length
let stack = []
let num = Array(len).fill()
for (let i = 0; i < len; i++) {
if (compareVals[i] > 0) {
stack.push([i, stackVals[i]])
} else {
num[i] = stack.pop()
num[i][1] += compareVals[i]
num[num[i][0]] = [i, -num[i][1]]
}
}
for (let i = 0; i < len; i++) {
if (num[i][0] > i) {
num[i] = part2 ? Math.max(1, 1 + num[i][1]) : Math.min(9, 9 + num[i][1])
} else {
num[i] = num[num[i][0]] + num[i][1]
}
}
return num.join("")
}
if (typeof window == "undefined") {
module.exports = day24
}

36
2021/25.js Normal file
View File

@@ -0,0 +1,36 @@
function step(grid, dir) {
let moved = false
grid.mapMut((e, pt, g) => {
let target = dir == 1 ?
new Point((pt.x + 1) % g.width, pt.y) :
new Point(pt.x, (pt.y + 1) % g.height)
if (e % 3 == dir && g.get(target) % 3 == 0) {
g.set(target, dir * 3)
moved = true
return e
} else {
return e * 3 + e
}
}).mapMut((e) => ((e / 3) | 0) % 3)
return moved
}
function day25(input) {
let grid = Grid.fromStr(input).mapMut((e) => ".>v".indexOf(e))
let moved
let steps = 0
do {
steps++
} while (step(grid, 1) + step(grid, 2))
return steps
}
if (typeof window == "undefined") {
module.exports = day25
}

2
out.js
View File

@@ -1244,7 +1244,7 @@ if (typeof window == "undefined" && process.argv[2] == "test") {
const year = "2021"
for (let i = +process.argv[3] || 1; i <= 23; i++) {
for (let i = +process.argv[3] || 1; i <= 25; i++) {
const func = require(`./${year}/${i}.js`)
const input = fs.readFileSync(`./${year}/inputs/${i}`, "utf8")
const answers = fs.readFileSync(`./${year}/answers/${i}`, "utf8").split("\n-----\n")

View File

@@ -3,7 +3,7 @@ if (typeof window == "undefined" && process.argv[2] == "test") {
const year = "2021"
for (let i = +process.argv[3] || 1; i <= 23; i++) {
for (let i = +process.argv[3] || 1; i <= 25; i++) {
const func = require(`./${year}/${i}.js`)
const input = fs.readFileSync(`./${year}/inputs/${i}`, "utf8")
const answers = fs.readFileSync(`./${year}/answers/${i}`, "utf8").split("\n-----\n")