From a7711439913ca8987d61d8e5bc9181e54c1fcd1b Mon Sep 17 00:00:00 2001 From: Joshua Seigler Date: Mon, 6 Dec 2021 01:15:32 -0500 Subject: [PATCH] day 6 --- .aocrunner.json | 12 ++++++------ README.md | 12 ++++++------ src/day06/README.md | 9 +++++++++ src/day06/index.js | 42 ++++++++++++++++++++++++++++++++++++++++++ src/utils/index.js | 21 +++++++++++++++++++++ 5 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 src/day06/README.md create mode 100644 src/day06/index.js diff --git a/.aocrunner.json b/.aocrunner.json index a543e98..aa06d85 100644 --- a/.aocrunner.json +++ b/.aocrunner.json @@ -81,16 +81,16 @@ }, { "part1": { - "solved": false, - "result": null, + "solved": true, + "result": "386755", "attempts": [], - "time": null + "time": 24.88 }, "part2": { - "solved": false, - "result": null, + "solved": true, + "result": "1732731810807", "attempts": [], - "time": null + "time": 0.2 } }, { diff --git a/README.md b/README.md index c42eea2..6e82c40 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ [![Day](https://badgen.net/badge/03/%E2%98%85%E2%98%85/green)](src/day03) [![Day](https://badgen.net/badge/04/%E2%98%85%E2%98%85/green)](src/day04) [![Day](https://badgen.net/badge/05/%E2%98%85%E2%98%85/green)](src/day05) -![Day](https://badgen.net/badge/06/%E2%98%86%E2%98%86/gray) +[![Day](https://badgen.net/badge/06/%E2%98%85%E2%98%85/green)](src/day06) ![Day](https://badgen.net/badge/07/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/08/%E2%98%86%E2%98%86/gray) ![Day](https://badgen.net/badge/09/%E2%98%86%E2%98%86/gray) @@ -104,9 +104,9 @@ Both parts: 217.70999999999998ms ``` Day 06 -Time part 1: - -Time part 2: - -Both parts: - +Time part 1: 24.88ms +Time part 2: 0.2ms +Both parts: 25.08ms ``` ``` @@ -243,8 +243,8 @@ Both parts: - ``` ``` -Total stars: 10/50 -Total time: 306.71ms +Total stars: 12/50 +Total time: 331.78999999999996ms ``` diff --git a/src/day06/README.md b/src/day06/README.md new file mode 100644 index 0000000..98bb6d8 --- /dev/null +++ b/src/day06/README.md @@ -0,0 +1,9 @@ +# 🎄 Advent of Code 2021 - day 6 🎄 + +## Info + +Task description: [link](https://adventofcode.com/2021/day/6) + +## Notes + +... \ No newline at end of file diff --git a/src/day06/index.js b/src/day06/index.js new file mode 100644 index 0000000..2b6e14e --- /dev/null +++ b/src/day06/index.js @@ -0,0 +1,42 @@ +import run from "aocrunner" +import { memo } from "../utils/index.js" + +const parseInput = (rawInput) => rawInput.trim().split`,`.map((x) => +x) + +const part1 = (rawInput) => { + const input = parseInput(rawInput) + for (let i = 0; i < 80; i++) { + const length = input.length + for (let j = 0; j < length; j++) { + if (input[j]-- === 0) { + input[j] = 6 + input.push(8) + } + } + } + return input.length +} + +const part2 = (rawInput) => { + const input = parseInput(rawInput) + const bins = new Array(9).fill(0) + input.forEach((f) => bins[f]++) + for (let i = 0; i < 256; i++) { + const spawning = bins.shift() + bins.push(spawning) + bins[6] += spawning + } + return bins.reduce((acc, x) => acc + x, 0) +} + +run({ + part1: { + tests: [{ input: `3,4,3,1,2`, expected: 5934 }], + solution: part1, + }, + part2: { + tests: [{ input: `3,4,3,1,2`, expected: 26984457539 }], + solution: part2, + }, + trimTestInputs: true, +}) diff --git a/src/utils/index.js b/src/utils/index.js index 6619010..9a139fc 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -34,3 +34,24 @@ export function sequence(a, b) { } return s } + +export function memo(fn) { + const cache = {} + + function get(args) { + let node = cache + for (const arg of args) { + if (!("next" in node)) node.next = new Map() + if (!node.next.has(arg)) node.next.set(arg, {}) + node = node.next.get(arg) + } + return node + } + + return function (...args) { + const cache = get(args) + if ("item" in cache) return cache.item + cache.item = fn(...args) + return cache.item + } +}