cleanup
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
function day1(input, part2) {
|
||||
return input.split`\n`.num().count((e, i, a, k = a[i - (part2 ? 3 : 1)]) => k && e > k)
|
||||
return input.split("\n").num().count((e, i, a, k = a[i - (part2 ? 3 : 1)]) => k && e > k)
|
||||
}
|
||||
|
||||
if (typeof window == "undefined") {
|
||||
|
||||
10
2021/10.js
10
2021/10.js
@@ -13,18 +13,14 @@ function day10(input, part2) {
|
||||
if (opens.includes(char)) {
|
||||
stack.push(closes[opens.indexOf(char)])
|
||||
} else if (char != stack.pop()) {
|
||||
return scores1[char]
|
||||
return { error: true, score: scores1[char] }
|
||||
}
|
||||
}
|
||||
|
||||
return stack.reverse()
|
||||
return { error: false, score: stack.reduceRight((a, b) => a * 5 + scores2[b], 0) }
|
||||
})
|
||||
|
||||
if (!part2) {
|
||||
return results.filter((e) => !Array.isArray(e)).sum()
|
||||
} else {
|
||||
return results.filter((e) => Array.isArray(e)).map((e) => e.reduce((a, b) => a * 5 + scores2[b], 0)).medianNumeric()
|
||||
}
|
||||
return results.filter((e) => part2 ^ e.error).map((e) => e.score)[part2 ? "medianNumeric" : "sum"]()
|
||||
}
|
||||
|
||||
if (typeof window == "undefined") {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
function day13(input, part2) {
|
||||
input = input.split("\n").splitOnElement("")
|
||||
let lines = 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]])
|
||||
let points = lines[0].map((e) => new Point(...e.split(",").num())).pt
|
||||
let instructions = lines[1].map((e) => e.split(" ").last.split("=")).map((e) => [e[0], +e[1]])
|
||||
|
||||
for (let instruction of instructions) {
|
||||
let pos = instruction[1]
|
||||
|
||||
26
2021/14.js
26
2021/14.js
@@ -1,8 +1,12 @@
|
||||
function day14(input, part2) {
|
||||
input = input.split("\n").splitOnElement("")
|
||||
function getNum(letter) {
|
||||
return letter.charCodeAt(0) - "A".charCodeAt(0)
|
||||
}
|
||||
|
||||
let init = input[0][0].split("")
|
||||
let rules = input[1].map((e) => e.split(" -> "))
|
||||
function day14(input, part2) {
|
||||
let lines = input.split("\n").splitOnElement("")
|
||||
|
||||
let init = lines[0][0].split("")
|
||||
let rules = lines[1].map((e) => e.split(" -> "))
|
||||
|
||||
let counts = {}
|
||||
|
||||
@@ -29,11 +33,19 @@ function day14(input, part2) {
|
||||
}
|
||||
}
|
||||
|
||||
counts = Object.keys(counts).map((key) => [key.split(""), counts[key]])
|
||||
// this double counts all letters except the first and last so we add one to those and / 2 at end
|
||||
let letters = Array(26).fill(0)
|
||||
letters[getNum(init[0])]++
|
||||
letters[getNum(init[init.length - 1])]++
|
||||
|
||||
let letters = counts.flatMap((e) => e[0]).uniq().map((letter) => (counts.filter((e) => e[0].includes(letter)).map((e) => e[0].count(letter) * e[1]).sum() + (letter == init[0] || letter == init[init.length - 1])) / 2)
|
||||
for (let pair in counts) {
|
||||
letters[getNum(pair[0])] += counts[pair]
|
||||
letters[getNum(pair[1])] += counts[pair]
|
||||
}
|
||||
|
||||
return letters.max() - letters.min()
|
||||
letters = letters.truthy()
|
||||
|
||||
return (letters.max() - letters.min()) / 2
|
||||
}
|
||||
|
||||
if (typeof window == "undefined") {
|
||||
|
||||
12
2021/2.js
12
2021/2.js
@@ -3,13 +3,17 @@ function day2(input, part2) {
|
||||
let y = 0
|
||||
let a = 0
|
||||
|
||||
input.split`\n`.forEach((e) => {
|
||||
for (let line of input.split("\n")) {
|
||||
if (!part2) {
|
||||
eval(e.replace("forward", "x +=").replace("up", "y -=").replace("down", "y +="))
|
||||
eval(line.replace("forward", "x +=")
|
||||
.replace("up", "y -=")
|
||||
.replace("down", "y +="))
|
||||
} else {
|
||||
eval(e.replace(/forward (.+)/, "x += $1; y += a * $1").replace("up", "a -=").replace("down", "a +="))
|
||||
eval(line.replace(/forward (.+)/, "x += $1; y += a * $1")
|
||||
.replace("up", "a -=")
|
||||
.replace("down", "a +="))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return x * y
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ class Cuboid {
|
||||
}
|
||||
|
||||
function day20(input, part2) {
|
||||
let lines = input.split`\n`
|
||||
let lines = input.split("\n")
|
||||
|
||||
let cuboids = []
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ function day3(input, part2) {
|
||||
if (!part2) {
|
||||
let gamma = g.getColumns().map((col) => col.mode())
|
||||
let epsilon = g.getColumns().map((col) => col.antimode())
|
||||
return parseInt(gamma.join``, 2) * parseInt(epsilon.join``, 2)
|
||||
return parseInt(gamma.join(""), 2) * parseInt(epsilon.join(""), 2)
|
||||
} else {
|
||||
let oxy = g.getRows()
|
||||
let co2 = g.getRows()
|
||||
@@ -16,7 +16,7 @@ function day3(input, part2) {
|
||||
co2 = co2.filter((e) => e[i] == co2Bit)
|
||||
}
|
||||
|
||||
return parseInt(oxy[0].join``, 2) * parseInt(co2[0].join``, 2)
|
||||
return parseInt(oxy[0].join(""), 2) * parseInt(co2[0].join(""), 2)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
function day4(input, part2) {
|
||||
input = input.split("\n")
|
||||
let lines = input.split("\n")
|
||||
|
||||
let seq = input.shift().split(",").num()
|
||||
let grids = input.splitOnElement("").filter((e) => e.length).map((grid) => Grid.fromStr(grid.map((line) => line.replace(/^ /, "")).join("\n"), /\s+/).mapMut((e) => [+e, false]))
|
||||
let seq = lines.shift().split(",").num()
|
||||
let grids = lines.splitOnElement("").filter((e) => e.length).map((grid) => Grid.fromStr(grid.map((line) => line.replace(/^ /, "")).join("\n"), /\s+/).mapMut((e) => [+e, false]))
|
||||
|
||||
let score
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
function day6(input, part2) {
|
||||
input = input.split(",").num()
|
||||
|
||||
let counts = Array(9).fill().map((_, i) => input.count(i))
|
||||
let fish = input.split(",").num()
|
||||
let counts = Array(9).fill().map((_, i) => fish.count(i))
|
||||
|
||||
for (let i = 0; i < (part2 ? 256 : 80); i++) {
|
||||
counts.push(counts.shift())
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
function day7(input, part2) {
|
||||
input = input.split(",").num()
|
||||
let crabs = input.split(",").num()
|
||||
|
||||
let max = input.max()
|
||||
let max = crabs.max()
|
||||
let costs = []
|
||||
|
||||
for (let i = 0; i < max; i++) {
|
||||
costs[i] = input.map((e) => {
|
||||
costs[i] = crabs.map((e) => {
|
||||
let dist = Math.abs(e - i)
|
||||
return part2 ? dist * (dist + 1) / 2 : dist
|
||||
}).sum()
|
||||
|
||||
9
graph.js
9
graph.js
@@ -77,6 +77,8 @@ Node = class Node {
|
||||
let visited = {}
|
||||
let toVisit = [this]
|
||||
|
||||
let i = 0
|
||||
|
||||
while (toVisit.length) {
|
||||
let toVisitNew = []
|
||||
|
||||
@@ -92,13 +94,16 @@ Node = class Node {
|
||||
})
|
||||
|
||||
toVisit = toVisitNew
|
||||
console.log(toVisit.length)
|
||||
|
||||
if (++i % 100 == 0) {
|
||||
console.log(toVisit.length)
|
||||
}
|
||||
}
|
||||
|
||||
console.timeEnd("heap gen")
|
||||
console.time("search")
|
||||
|
||||
let i = 0
|
||||
i = 0
|
||||
|
||||
while (heap.data.length) {
|
||||
let min = heap.extract()
|
||||
|
||||
9
out.js
9
out.js
@@ -761,6 +761,8 @@ Node = class Node {
|
||||
let visited = {}
|
||||
let toVisit = [this]
|
||||
|
||||
let i = 0
|
||||
|
||||
while (toVisit.length) {
|
||||
let toVisitNew = []
|
||||
|
||||
@@ -776,13 +778,16 @@ Node = class Node {
|
||||
})
|
||||
|
||||
toVisit = toVisitNew
|
||||
console.log(toVisit.length)
|
||||
|
||||
if (++i % 100 == 0) {
|
||||
console.log(toVisit.length)
|
||||
}
|
||||
}
|
||||
|
||||
console.timeEnd("heap gen")
|
||||
console.time("search")
|
||||
|
||||
let i = 0
|
||||
i = 0
|
||||
|
||||
while (heap.data.length) {
|
||||
let min = heap.extract()
|
||||
|
||||
Reference in New Issue
Block a user