From 1e65e391236427c654e9c76f9d0691088694b890 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Mon, 13 Dec 2021 11:03:09 -0500 Subject: [PATCH] working! --- 2021/13/part1.mjs | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 2021/13/part2.mjs | 14 ++++++++++ 2021/13/sample | 21 ++++++++++++++ 2021/13/test.mjs | 28 +++++++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 2021/13/part1.mjs create mode 100644 2021/13/part2.mjs create mode 100644 2021/13/sample create mode 100644 2021/13/test.mjs diff --git a/2021/13/part1.mjs b/2021/13/part1.mjs new file mode 100644 index 0000000..74fa80c --- /dev/null +++ b/2021/13/part1.mjs @@ -0,0 +1,70 @@ +import fs from "fs-extra"; +import fp from "lodash/fp.js"; +import _ from "lodash"; + +export const processInput = (input) => fs.readFile(input,'utf8').then( + content => { + let [ coords, instructions ] = content.split("\n\n"); + + let grid = []; + + for( const [y,x] of coords.split("\n").map(line => line.split(",").map( x => parseInt(x) )) ) { + if(!grid[x]) grid[x] = []; + grid[x][y] = true; + } + + grid = [...grid]; + grid = grid.map( x => x? [...x]:[] ); + + instructions = instructions.split("\n").filter(x=>x).map( line => line.match(/([xy])=(\d+)/) ) + .map( ([_,direction,level]) => ([direction,parseInt(level)]) ); + + return { + grid, + instructions, + } + + } +); + +export function foldGrid(grid,direction,level) { + if(direction === 'x') { + for( const row of grid ) { + if(!row) continue; + const folded = row.splice(level); + folded.forEach((v,i) => { + if(i===0) return; + if(!v) return; + row[level-i] = v; + }) + } + + return grid; + } + + const folded = grid.splice(level); + folded.forEach((v,i) => { + if(i===0) return; + if(!v) return; + grid[level-i] = + _.range(_.max([grid[level-i].length,folded[i].length])). + map( j => grid[level-i][j] || folded[i][j] ); + }) + + return grid; +} + +export function printGrid(grid) { + return grid.map( line => + (line||[]).map( x => x ? '#':'.' ).join('') + ).join("\n"); +} + +export function solution({grid, instructions}) { + + foldGrid(grid,...instructions.shift()); + + return _.sum(grid.flat()); + + +} diff --git a/2021/13/part2.mjs b/2021/13/part2.mjs new file mode 100644 index 0000000..afdbfa6 --- /dev/null +++ b/2021/13/part2.mjs @@ -0,0 +1,14 @@ +import fs from "fs-extra"; +import fp from "lodash/fp.js"; +import _ from "lodash"; + +import * as p1 from './part1.mjs'; + +export function solution({grid, instructions}) { + + instructions.forEach(i =>p1.foldGrid(grid,...i)); + + return p1.printGrid(grid); + + +} diff --git a/2021/13/sample b/2021/13/sample new file mode 100644 index 0000000..282114c --- /dev/null +++ b/2021/13/sample @@ -0,0 +1,21 @@ +6,10 +0,14 +9,10 +0,3 +10,4 +4,11 +6,0 +6,12 +4,1 +0,13 +10,12 +3,4 +3,0 +8,4 +1,10 +2,14 +8,10 +9,0 + +fold along y=7 +fold along x=5 diff --git a/2021/13/test.mjs b/2021/13/test.mjs new file mode 100644 index 0000000..63f3239 --- /dev/null +++ b/2021/13/test.mjs @@ -0,0 +1,28 @@ +// https://adventofcode.com/2021/day/13 + +import tap from "tap"; +import fs from "fs-extra"; + +import * as p1 from "./part1.mjs"; +import * as p2 from "./part2.mjs"; + +const sample = p1.processInput('sample'); +const input = p1.processInput('input'); + +tap.test("part1", async (t) => { + const x = await sample; + t.equal(p1.solution(x), 17); + t.equal(p1.solution(x), 16); + t.equal(p1.solution(await input), 745); +}); + +tap.test("part2", async (t) => { + // t.equal(p2.solution(await sample), 0); + t.equal(p2.solution(await input), + `.##..###..#..#...##.####.###...##...##.. +#..#.#..#.#.#.....#.#....#..#.#..#.#..#. +#..#.###..##......#.###..###..#....#.... +####.#..#.#.#.....#.#....#..#.#.##.#.... +#..#.#..#.#.#..#..#.#....#..#.#..#.#..#. +#..#.###..#..#..##..#....###...###..##..`); +});