This commit is contained in:
Joshua Seigler 2021-12-11 00:41:11 -05:00
parent 760409604d
commit 1c4a3cfcd8
4 changed files with 123 additions and 16 deletions

View file

@ -153,16 +153,16 @@
},
{
"part1": {
"solved": false,
"result": null,
"solved": true,
"result": "1741",
"attempts": [],
"time": null
"time": 9.6
},
"part2": {
"solved": false,
"result": null,
"solved": true,
"result": "440",
"attempts": [],
"time": null
"time": 1.69
}
},
{

View file

@ -20,8 +20,8 @@
[![Day](https://badgen.net/badge/07/%E2%98%85%E2%98%85/green)](src/day07)
[![Day](https://badgen.net/badge/08/%E2%98%85%E2%98%85/green)](src/day08)
[![Day](https://badgen.net/badge/09/%E2%98%85%E2%98%85/green)](src/day09)
![Day](https://badgen.net/badge/10/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/11/%E2%98%86%E2%98%86/gray)
[![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%86%E2%98%86/gray)
![Day](https://badgen.net/badge/13/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/14/%E2%98%86%E2%98%86/gray)
@ -132,16 +132,16 @@ Both parts: 34.98ms
```
Day 10
Time part 1: -
Time part 2: -
Both parts: -
Time part 1: 7.37ms
Time part 2: 2.29ms
Both parts: 9.66ms
```
```
Day 11
Time part 1: -
Time part 2: -
Both parts: -
Time part 1: 9.6ms
Time part 2: 1.69ms
Both parts: 11.29ms
```
```
@ -243,8 +243,8 @@ Both parts: -
```
```
Total stars: 18/50
Total time: 433.29999999999995ms
Total stars: 22/50
Total time: 454.25ms
```
<!--/RESULTS-->

9
src/day11/README.md Normal file
View file

@ -0,0 +1,9 @@
# 🎄 Advent of Code 2021 - day 11 🎄
## Info
Task description: [link](https://adventofcode.com/2021/day/11)
## Notes
...

98
src/day11/index.js Normal file
View file

@ -0,0 +1,98 @@
import run from "aocrunner"
const parseInput = (rawInput) =>
rawInput.split`\n`.map((l) => l.split``.map((x) => +x))
const part1 = (rawInput, isPartTwo = false) => {
const input = parseInput(rawInput)
const h = input.length
const w = input[0].length
let flashes = 0,
flashesThisRound
for (let step = 0; step < 100 || isPartTwo; step++) {
flashesThisRound = 0
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
input[y][x]++
}
}
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
if (input[y][x] > 9) {
bump(y, x)
}
}
}
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
if (isNaN(input[y][x])) {
input[y][x] = 0
}
}
}
if (flashesThisRound === w * h && isPartTwo) {
return step + 1
}
}
function bump(y, x) {
if (y < 0 || y >= h || x < 0 || x >= w) return
if (++input[y][x] > 9) {
input[y][x] = NaN
flashes++
flashesThisRound++
bump(y - 1, x - 1)
bump(y - 1, x)
bump(y - 1, x + 1)
bump(y, x - 1)
bump(y, x + 1)
bump(y + 1, x - 1)
bump(y + 1, x)
bump(y + 1, x + 1)
}
}
return flashes
}
const part2 = (rawInput) => {
return part1(rawInput, true)
}
run({
part1: {
tests: [
{
input: `5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526`,
expected: 1656,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526`,
expected: 195,
},
],
solution: part2,
},
trimTestInputs: true,
})