From 86f43b8e5164b46fc7b554a2a06daf6e33694994 Mon Sep 17 00:00:00 2001 From: nim-ka Date: Tue, 22 Nov 2022 23:01:38 +0000 Subject: [PATCH] . --- 2021/19.js | 26 +++++++++++++------------- 2021/20.js | 29 +++++++++++++++++++++++++++++ out.js | 2 +- test.js | 2 +- 4 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 2021/20.js diff --git a/2021/19.js b/2021/19.js index dc67a62..4fbb7c8 100644 --- a/2021/19.js +++ b/2021/19.js @@ -62,6 +62,8 @@ class Transform { } } +// this sucks on another level but hey it works ? +// obviously i should make the initial search recursive instead of looping through all combinations but ghghghhghghg function day19(input, part2) { let lines = input.split("\n") @@ -87,13 +89,9 @@ function day19(input, part2) { let scan = scans[i] for (let j = 0; j < scan.length; j++) { - if (scan[j].link) { - continue - } - let scanRel = scan.map((e) => e.sub(scan[j])) - out: for (let k = i + 1; k < scans.length; k++) { + for (let k = i + 1; k < scans.length; k++) { if (transforms[i][k]) { continue } @@ -102,7 +100,7 @@ function day19(input, part2) { continue } - for (let t2 = 0; t2 < 24; t2++) { + out: for (let t2 = 0; t2 < 24; t2++) { let scan2 = scans[k].map((e) => rotate(e, t2)) for (let l = 0; l < scan2.length; l++) { @@ -113,15 +111,20 @@ function day19(input, part2) { if (overlaps.length >= 12) { console.log([i, j, k, l, t2]) - tree[i].push(k) - tree[k].push(i) + for (let overlap of overlaps) { + scan[scanRel.indexOf(overlap)].linked = k + scan2[scanRel2.indexOf(overlap)].linked = i + } + + tree[i].pushUniq(k, ...tree[k]) + tree[k].pushUniq(i, ...tree[i]) if (tree[0].includes(i)) { - tree[0].pushUniq(k) + tree[0].pushUniq(...tree[i]) } if (tree[0].includes(k)) { - tree[0].pushUniq(i) + tree[0].pushUniq(...tree[k]) } transforms[i][k] = new Transform(scan2[l].sub(scan[j]), inverseRotations[t2]) @@ -137,7 +140,6 @@ function day19(input, part2) { } } - // this sucks on another level but hey it works ? Array(transforms.length).fill().forEach((_, i) => { (function recurse(i) { for (let j = 0; j < transforms[i].length; j++) { @@ -153,8 +155,6 @@ function day19(input, part2) { transforms[i][k] = transforms[i][j].compose(transforms[j][k]) transforms[k][i] = transforms[i][k].invert() - console.log(`filled in ${i}<->${k} from ${i}<->${j}<->${k}`) - recurse(j) } } diff --git a/2021/20.js b/2021/20.js new file mode 100644 index 0000000..bea524d --- /dev/null +++ b/2021/20.js @@ -0,0 +1,29 @@ +function isEdge(pt, g) { + return pt.x == 0 || pt.x == g.width - 1 || pt.y == 0 || pt.y == g.height - 1 +} + +function expand(grid, fill) { + return new Grid(grid.width + 2, grid.height + 2).mapMut((_, pt, g) => isEdge(pt, g) ? fill : grid.get(pt.ul())) +} + +function day20(input, part2) { + let lines = input.split("\n").splitOnElement("") + + let code = lines[0][0].split("").map((e) => e == "#") + + let width = lines[1][0].length + let height = lines[1].length + + let grid = expand(expand(Grid.fromArr(lines[1]).mapMut((e) => e == "#"), false), false) + + for (let i = 0; i < (part2 ? 50 : 2); i++) { + grid = new Grid(grid.width, grid.height).mapMut((_, pt, g) => code[pt.getUnfilteredAllNeighborsIncSelf().map((pt2) => grid.get(grid.contains(pt2) ? pt2 : pt)).reduce((a, b) => 2 * a + b)]) + grid = expand(grid, grid.get(new Point(0, 0))) + } + + return grid.findAll((e) => e).length +} + +if (typeof window == "undefined") { + module.exports = day20 +} diff --git a/out.js b/out.js index 0e7c38f..ea865de 100644 --- a/out.js +++ b/out.js @@ -1244,7 +1244,7 @@ if (typeof window == "undefined" && process.argv[2] == "test") { const year = "2021" - for (let i = +process.argv[3] || 1; i <= 19; i++) { + for (let i = +process.argv[3] || 1; i <= 20; 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") diff --git a/test.js b/test.js index d55b65e..a215a04 100644 --- a/test.js +++ b/test.js @@ -3,7 +3,7 @@ if (typeof window == "undefined" && process.argv[2] == "test") { const year = "2021" - for (let i = +process.argv[3] || 1; i <= 19; i++) { + for (let i = +process.argv[3] || 1; i <= 20; 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")