.
This commit is contained in:
21
2021/3.js
Normal file
21
2021/3.js
Normal file
@@ -0,0 +1,21 @@
|
||||
function day3(input, part2) {
|
||||
let g = Grid.fromStr(input)
|
||||
|
||||
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)
|
||||
} else {
|
||||
let oxy = g.getRows()
|
||||
let co2 = g.getRows()
|
||||
|
||||
for (let i = 0; i < g.width; i++) {
|
||||
let oxyBit = oxy.transpose()[i].mode((a, b) => a == 1 ? -1 : 1)
|
||||
let co2Bit = co2.transpose()[i].antimode((a, b) => a == 0 ? -1 : 1)
|
||||
oxy = oxy.filter((e) => e[i] == oxyBit)
|
||||
co2 = co2.filter((e) => e[i] == co2Bit)
|
||||
}
|
||||
|
||||
return parseInt(oxy[0].join``, 2) * parseInt(co2[0].join``, 2)
|
||||
}
|
||||
}
|
||||
34
2021/4.js
Normal file
34
2021/4.js
Normal file
@@ -0,0 +1,34 @@
|
||||
function day4(input, part2) {
|
||||
input = 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 score
|
||||
|
||||
for (let num of seq) {
|
||||
for (let grid of grids) {
|
||||
if (grid.won) {
|
||||
continue
|
||||
}
|
||||
|
||||
let pt = grid.findIndex((e) => e[0] == num)
|
||||
|
||||
if (pt == Point.NONE) {
|
||||
continue
|
||||
}
|
||||
|
||||
grid.set(pt, [num, true])
|
||||
|
||||
if ([...grid.getRows(), ...grid.getColumns()].some((row) => row.every((e) => e[1]))) {
|
||||
grid.won = true
|
||||
|
||||
if (!score || part2) {
|
||||
score = grid.findAll((e) => !e[1]).map((e) => e[0]).sum() * num
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return score
|
||||
}
|
||||
Reference in New Issue
Block a user