prettier
This commit is contained in:
parent
1e65e39123
commit
360fc700b1
@ -2,33 +2,36 @@ import fs from "fs-extra";
|
|||||||
import fp from "lodash/fp.js";
|
import fp from "lodash/fp.js";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
|
|
||||||
export const processInput = (input) => fs.readFile(input,'utf8').then(
|
export const processInput = (input) =>
|
||||||
content => {
|
fs.readFile(input, "utf8").then((content) => {
|
||||||
let [coords, instructions] = content.split("\n\n");
|
let [coords, instructions] = content.split("\n\n");
|
||||||
|
|
||||||
let grid = [];
|
let grid = [];
|
||||||
|
|
||||||
for( const [y,x] of coords.split("\n").map(line => line.split(",").map( x => parseInt(x) )) ) {
|
for (const [y, x] of coords
|
||||||
|
.split("\n")
|
||||||
|
.map((line) => line.split(",").map((x) => parseInt(x)))) {
|
||||||
if (!grid[x]) grid[x] = [];
|
if (!grid[x]) grid[x] = [];
|
||||||
grid[x][y] = true;
|
grid[x][y] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
grid = [...grid];
|
grid = [...grid];
|
||||||
grid = grid.map( x => x? [...x]:[] );
|
grid = grid.map((x) => (x ? [...x] : []));
|
||||||
|
|
||||||
instructions = instructions.split("\n").filter(x=>x).map( line => line.match(/([xy])=(\d+)/) )
|
instructions = instructions
|
||||||
.map( ([_,direction,level]) => ([direction,parseInt(level)]) );
|
.split("\n")
|
||||||
|
.filter((x) => x)
|
||||||
|
.map((line) => line.match(/([xy])=(\d+)/))
|
||||||
|
.map(([_, direction, level]) => [direction, parseInt(level)]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
grid,
|
grid,
|
||||||
instructions,
|
instructions,
|
||||||
}
|
};
|
||||||
|
});
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
export function foldGrid(grid, direction, level) {
|
export function foldGrid(grid, direction, level) {
|
||||||
if(direction === 'x') {
|
if (direction === "x") {
|
||||||
for (const row of grid) {
|
for (const row of grid) {
|
||||||
if (!row) continue;
|
if (!row) continue;
|
||||||
const folded = row.splice(level);
|
const folded = row.splice(level);
|
||||||
@ -36,7 +39,7 @@ export function foldGrid(grid,direction,level) {
|
|||||||
if (i === 0) return;
|
if (i === 0) return;
|
||||||
if (!v) return;
|
if (!v) return;
|
||||||
row[level - i] = v;
|
row[level - i] = v;
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return grid;
|
return grid;
|
||||||
@ -46,25 +49,22 @@ export function foldGrid(grid,direction,level) {
|
|||||||
folded.forEach((v, i) => {
|
folded.forEach((v, i) => {
|
||||||
if (i === 0) return;
|
if (i === 0) return;
|
||||||
if (!v) return;
|
if (!v) return;
|
||||||
grid[level-i] =
|
grid[level - i] = _.range(
|
||||||
_.range(_.max([grid[level-i].length,folded[i].length])).
|
_.max([grid[level - i].length, folded[i].length])
|
||||||
map( j => grid[level-i][j] || folded[i][j] );
|
).map((j) => grid[level - i][j] || folded[i][j]);
|
||||||
})
|
});
|
||||||
|
|
||||||
return grid;
|
return grid;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function printGrid(grid) {
|
export function printGrid(grid) {
|
||||||
return grid.map( line =>
|
return grid
|
||||||
(line||[]).map( x => x ? '#':'.' ).join('')
|
.map((line) => (line || []).map((x) => (x ? "#" : ".")).join(""))
|
||||||
).join("\n");
|
.join("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
export function solution({ grid, instructions }) {
|
export function solution({ grid, instructions }) {
|
||||||
|
|
||||||
foldGrid(grid, ...instructions.shift());
|
foldGrid(grid, ...instructions.shift());
|
||||||
|
|
||||||
return _.sum(grid.flat());
|
return _.sum(grid.flat());
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,13 +2,10 @@ import fs from "fs-extra";
|
|||||||
import fp from "lodash/fp.js";
|
import fp from "lodash/fp.js";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
|
|
||||||
import * as p1 from './part1.mjs';
|
import * as p1 from "./part1.mjs";
|
||||||
|
|
||||||
export function solution({ grid, instructions }) {
|
export function solution({ grid, instructions }) {
|
||||||
|
instructions.forEach((i) => p1.foldGrid(grid, ...i));
|
||||||
instructions.forEach(i =>p1.foldGrid(grid,...i));
|
|
||||||
|
|
||||||
return p1.printGrid(grid);
|
return p1.printGrid(grid);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,8 @@ import fs from "fs-extra";
|
|||||||
import * as p1 from "./part1.mjs";
|
import * as p1 from "./part1.mjs";
|
||||||
import * as p2 from "./part2.mjs";
|
import * as p2 from "./part2.mjs";
|
||||||
|
|
||||||
const sample = p1.processInput('sample');
|
const sample = p1.processInput("sample");
|
||||||
const input = p1.processInput('input');
|
const input = p1.processInput("input");
|
||||||
|
|
||||||
tap.test("part1", async (t) => {
|
tap.test("part1", async (t) => {
|
||||||
const x = await sample;
|
const x = await sample;
|
||||||
@ -18,11 +18,13 @@ tap.test("part1", async (t) => {
|
|||||||
|
|
||||||
tap.test("part2", async (t) => {
|
tap.test("part2", async (t) => {
|
||||||
// t.equal(p2.solution(await sample), 0);
|
// t.equal(p2.solution(await sample), 0);
|
||||||
t.equal(p2.solution(await input),
|
t.equal(
|
||||||
|
p2.solution(await input),
|
||||||
`.##..###..#..#...##.####.###...##...##..
|
`.##..###..#..#...##.####.###...##...##..
|
||||||
#..#.#..#.#.#.....#.#....#..#.#..#.#..#.
|
#..#.#..#.#.#.....#.#....#..#.#..#.#..#.
|
||||||
#..#.###..##......#.###..###..#....#....
|
#..#.###..##......#.###..###..#....#....
|
||||||
####.#..#.#.#.....#.#....#..#.#.##.#....
|
####.#..#.#.#.....#.#....#..#.#.##.#....
|
||||||
#..#.#..#.#.#..#..#.#....#..#.#..#.#..#.
|
#..#.#..#.#.#..#..#.#....#..#.#..#.#..#.
|
||||||
#..#.###..#..#..##..#....###...###..##..`);
|
#..#.###..#..#..##..#....###...###..##..`
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user