mirror of
https://github.com/seigler/aoc2021
synced 2025-07-27 01:16:09 +00:00
day 6
This commit is contained in:
parent
7586d315fd
commit
a771143991
5 changed files with 84 additions and 12 deletions
|
@ -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
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
12
README.md
12
README.md
|
@ -16,7 +16,7 @@
|
|||
[](src/day03)
|
||||
[](src/day04)
|
||||
[](src/day05)
|
||||

|
||||
[](src/day06)
|
||||

|
||||

|
||||

|
||||
|
@ -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
|
||||
```
|
||||
|
||||
<!--/RESULTS-->
|
||||
|
|
9
src/day06/README.md
Normal file
9
src/day06/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2021 - day 6 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2021/day/6)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
42
src/day06/index.js
Normal file
42
src/day06/index.js
Normal file
|
@ -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,
|
||||
})
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue