From b0bf232079f5047c3ebda0312d6a683424be246c Mon Sep 17 00:00:00 2001 From: Joshua Seigler Date: Mon, 13 Dec 2021 01:17:44 -0500 Subject: [PATCH] day 13 --- .aocrunner.json | 12 +++--- README.md | 12 +++--- src/day13/README.md | 9 +++++ src/day13/index.js | 92 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 12 deletions(-) create mode 100644 src/day13/README.md create mode 100644 src/day13/index.js diff --git a/.aocrunner.json b/.aocrunner.json index c5f7fb5..3849615 100644 --- a/.aocrunner.json +++ b/.aocrunner.json @@ -181,16 +181,16 @@ }, { "part1": { - "solved": false, - "result": null, + "solved": true, + "result": "847", "attempts": [], - "time": null + "time": 3.59 }, "part2": { - "solved": false, - "result": null, + "solved": true, + "result": "BCZRCEAB", "attempts": [], - "time": null + "time": 17.85 } }, { diff --git a/README.md b/README.md index 6570e21..b77e0fb 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ [![Day](https://badgen.net/badge/10/%E2%98%85%E2%98%85/green)](src/day10) [![Day](https://badgen.net/badge/11/%E2%98%85%E2%98%85/green)](src/day11) [![Day](https://badgen.net/badge/12/%E2%98%85%E2%98%85/green)](src/day12) -![Day](https://badgen.net/badge/13/%E2%98%86%E2%98%86/gray) +[![Day](https://badgen.net/badge/13/%E2%98%85%E2%98%86/green)](src/day13) ![Day](https://badgen.net/badge/14/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/15/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/16/%E2%98%86%E2%98%86/gray) @@ -153,9 +153,9 @@ Both parts: 602.51ms ``` Day 13 -Time part 1: - +Time part 1: 3.8ms Time part 2: - -Both parts: - +Both parts: 3.8ms ``` ``` @@ -243,12 +243,12 @@ Both parts: - ``` ``` -Total stars: 24/50 -Total time: 1056.76ms +Total stars: 25/50 +Total time: 1060.56ms ``` --- -✨🎄🎁🎄🎅🎄🎁🎄✨ \ No newline at end of file +✨🎄🎁🎄🎅🎄🎁🎄✨ diff --git a/src/day13/README.md b/src/day13/README.md new file mode 100644 index 0000000..372aa23 --- /dev/null +++ b/src/day13/README.md @@ -0,0 +1,9 @@ +# 🎄 Advent of Code 2021 - day 13 🎄 + +## Info + +Task description: [link](https://adventofcode.com/2021/day/13) + +## Notes + +... \ No newline at end of file diff --git a/src/day13/index.js b/src/day13/index.js new file mode 100644 index 0000000..19910e3 --- /dev/null +++ b/src/day13/index.js @@ -0,0 +1,92 @@ +import run from "aocrunner" + +const parseInput = (rawInput) => { + const [Sdots, Sfolds] = rawInput.trim().split`\n\n` + return [ + new Set(Sdots.split`\n`), + Sfolds.split`\n`.map((x) => x.split(/[ =]/).splice(2)), + ] +} + +function fold(dots, n, axis) { + n = +n + const nextSet = new Set() + dots.forEach((dot) => { + const [x, y] = dot.split`,`.map((x) => +x) + if (axis == "x") { + nextSet.add(x <= n ? dot : `${2 * n - x},${y}`) + } else { + nextSet.add(y <= n ? dot : `${x},${2 * n - y}`) + } + }) + return nextSet +} + +const part1 = (rawInput) => { + const [dots, folds] = parseInput(rawInput) + let currentDots = dots + folds.slice(0, 1).forEach(([axis, n]) => { + currentDots = fold(currentDots, n, axis) + }) + return currentDots.size +} + +const part2 = (rawInput) => { + const [dots, folds] = parseInput(rawInput) + let currentDots = dots + folds.forEach(([axis, n]) => { + currentDots = fold(currentDots, n, axis) + }) + const field = [...currentDots].map((d) => d.split`,`.map((x) => +x)) + const [maxX, maxY] = field.reduce( + ([maxX, maxY], [x, y]) => [Math.max(x, maxX), Math.max(y, maxY)], + [0, 0], + ) + for (let y = 0; y <= maxY; y++) { + let line = "" + for (let x = 0; x <= maxX; x++) { + line += currentDots.has(`${x},${y}`) ? "█" : " " + } + console.log(line) + } + return +} + +run({ + part1: { + tests: [ + { + input: `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`, + expected: 17, + }, + ], + solution: part1, + }, + part2: { + tests: [ + // { input: ``, expected: "" }, + ], + solution: part2, + }, + trimTestInputs: true, +})