This commit is contained in:
nim-ka
2022-11-17 00:57:31 +00:00
parent 26120e2818
commit c42480ef0b
6 changed files with 133 additions and 12 deletions

34
2021/11.js Normal file
View File

@@ -0,0 +1,34 @@
function day11(input, part2) {
let grid = Grid.fromStr(input).mapMut((e) => ({ energy: +e, flashed: false }))
let steps = 0
let flashes = 0
let totalFlashes = 0
while (part2 ? flashes != grid.width * grid.height : steps < 100) {
flashes = 0
grid.forEach(function flash(e, pt, g) {
e.energy++
if (!e.flashed && e.energy > 9) {
e.flashed = true
flashes++
g.getAllNeighborsThat(pt, (e) => !e.flashed).forEach((pt) => flash(g.get(pt), pt, g))
}
})
grid.forEach((e) => {
if (e.flashed) {
e.energy = 0
e.flashed = false
}
})
steps++
totalFlashes += flashes
}
return part2 ? steps : totalFlashes
}

42
2021/12.js Normal file
View File

@@ -0,0 +1,42 @@
// TODO: not very efficient because i was lazy
function day12(input, part2) {
let cxns = input.split("\n").map((line) => line.split("-"))
let dict = {}
for (let cxn of cxns) {
dict[cxn[0]] = [...(dict[cxn[0]] || []), cxn[1]]
dict[cxn[1]] = [...(dict[cxn[1]] || []), cxn[0]]
}
let count = 0
let paths = [["start"]]
while (paths.length) {
let newPaths = []
for (let path of paths) {
for (let next of dict[path.last]) {
if (next == "start") {
continue
}
let newPath = [...path, next]
if (newPath.count((e, i, a) => e == e.toLowerCase() && a.indexOf(e) != i) > part2) {
continue
}
if (next == "end") {
count++
} else {
newPaths.push(newPath)
}
}
}
paths = newPaths
}
return count
}

27
2021/13.js Normal file
View File

@@ -0,0 +1,27 @@
function day13(input, part2) {
input = input.split("\n").splitOnElement("")
let points = input[0].map((e) => new Point(...e.split(",").num())).pt
let instructions = input[1].map((e) => e.split(" ").last.split("=")).map((e) => [e[0], +e[1]])
for (let instruction of instructions) {
let pos = instruction[1]
let newPoints = [].pt
for (let point of points) {
if (instruction[0] == "x") {
newPoints.push(point.x < pos ? point : new Point(2 * pos - point.x, point.y))
} else {
newPoints.push(point.y < pos ? point : new Point(point.x, 2 * pos - point.y))
}
}
points = newPoints.uniq()
if (!part2) {
return points.length
}
}
return new Grid(points.max((e) => e.x).x + 1, points.max((e) => e.y).y + 1).mapMut((_, pt) => pt.isIn(points) ? "#" : " ").toString("")
}