Skip to content

Commit

Permalink
2021.13
Browse files Browse the repository at this point in the history
  • Loading branch information
Doruk Gurleyen committed Dec 13, 2021
1 parent c735ab6 commit 4d60f30
Show file tree
Hide file tree
Showing 2 changed files with 963 additions and 0 deletions.
83 changes: 83 additions & 0 deletions challenges/2021/13.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Part 1
const solvePuzzle1 = (input) => {
let [paper, instructions] = getPaperAndInstructions(parseInput(input));

return fold(instructions[0], paper)
.flat()
.reduce((acc, val) => acc + val);
};

// Part 2
const solvePuzzle2 = (input) => {
let [paper, instructions] = getPaperAndInstructions(parseInput(input));
instructions.forEach((ins) => (paper = fold(ins, paper)));

return "\n" + paper.map((y) => y.reduce((acc, x) => (x ? acc + "#" : acc + " "), "")).join("\n");
};

const fold = ([dir, pos], paper) => {
if (dir === "y") {
const firstHalf = paper.slice(0, pos);
const secondHalf = paper.slice(pos + 1).reverse();
paper = [];

while (firstHalf.length > secondHalf.length) {
secondHalf.unshift(Array(firstHalf[0].length).fill(0));
}
while (firstHalf.length < secondHalf.length) {
firstHalf.unshift(Array(firstHalf[0].length).fill(0));
}

for (let i = 0; i < firstHalf.length; i++) {
paper.push([]);
for (let j = 0; j < firstHalf[i].length; j++) {
paper[i][j] = firstHalf[i][j] || secondHalf[i][j];
}
}
} else {
const firstHalf = Array.from({ length: paper.length }, () => []);
const secondHalf = Array.from({ length: paper.length }, () => []);

for (let i = 0; i < paper.length; i++) {
for (let j = 0; j < pos; j++) {
firstHalf[i].push(paper[i][j]);
}
for (let j = paper[i].length - 1; j > pos; j--) {
secondHalf[i].push(paper[i][j]);
}
}

paper = Array.from({ length: paper.length }, () => []);
for (let i = 0; i < firstHalf.length; i++) {
for (let j = 0; j < Math.max(firstHalf[i].length, secondHalf[i].length); j++) {
paper[i].push(Math.max(firstHalf[i][j] || secondHalf[i][j]));
}
}
}
return paper;
};

const getPaperAndInstructions = (input) => {
const [dots, instructions] = [[], []];
let [maxX, maxY] = [0, 0];

for (let line of input) {
if (line[0] === "f") {
const [direction, pos] = line.replace("fold along ", "").split("=");
instructions.push([direction, Number(pos)]);
} else {
const [x, y] = line.split(",").map((n) => Number(n));
dots.push([x, y]);
[maxX, maxY] = [Math.max(maxX, x), Math.max(maxY, y)];
}
}

const paper = Array.from({ length: maxY + 1 }, () => Array(maxX + 1).fill(0));
dots.forEach(([x, y]) => (paper[y][x] = 1));
return [paper, instructions];
};

const parseInput = (input) => input.split("\n").filter((x) => x !== "");

require(__dirname + "/../../utils/test.js").test(__filename, __dirname, solvePuzzle1);
require(__dirname + "/../../utils/test.js").test(__filename, __dirname, solvePuzzle2);
Loading

0 comments on commit 4d60f30

Please sign in to comment.