mirror of
https://github.com/seigler/aoc2021
synced 2025-07-27 01:16:09 +00:00
day 11
This commit is contained in:
parent
760409604d
commit
1c4a3cfcd8
4 changed files with 123 additions and 16 deletions
|
@ -153,16 +153,16 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"part1": {
|
"part1": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "1741",
|
||||||
"attempts": [],
|
"attempts": [],
|
||||||
"time": null
|
"time": 9.6
|
||||||
},
|
},
|
||||||
"part2": {
|
"part2": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "440",
|
||||||
"attempts": [],
|
"attempts": [],
|
||||||
"time": null
|
"time": 1.69
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
20
README.md
20
README.md
|
@ -20,8 +20,8 @@
|
||||||
[](src/day07)
|
[](src/day07)
|
||||||
[](src/day08)
|
[](src/day08)
|
||||||
[](src/day09)
|
[](src/day09)
|
||||||

|
[](src/day10)
|
||||||

|
[](src/day11)
|
||||||

|

|
||||||

|

|
||||||

|

|
||||||
|
@ -132,16 +132,16 @@ Both parts: 34.98ms
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 10
|
Day 10
|
||||||
Time part 1: -
|
Time part 1: 7.37ms
|
||||||
Time part 2: -
|
Time part 2: 2.29ms
|
||||||
Both parts: -
|
Both parts: 9.66ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 11
|
Day 11
|
||||||
Time part 1: -
|
Time part 1: 9.6ms
|
||||||
Time part 2: -
|
Time part 2: 1.69ms
|
||||||
Both parts: -
|
Both parts: 11.29ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -243,8 +243,8 @@ Both parts: -
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Total stars: 18/50
|
Total stars: 22/50
|
||||||
Total time: 433.29999999999995ms
|
Total time: 454.25ms
|
||||||
```
|
```
|
||||||
|
|
||||||
<!--/RESULTS-->
|
<!--/RESULTS-->
|
||||||
|
|
9
src/day11/README.md
Normal file
9
src/day11/README.md
Normal 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
98
src/day11/index.js
Normal 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,
|
||||||
|
})
|
Loading…
Add table
Add a link
Reference in a new issue