mirror of
https://github.com/seigler/aoc2021
synced 2025-07-27 01:16:09 +00:00
day 9
This commit is contained in:
parent
ac0d71598c
commit
22b50b4e1f
4 changed files with 104 additions and 12 deletions
|
@ -125,16 +125,16 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"part1": {
|
"part1": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "444",
|
||||||
"attempts": [],
|
"attempts": [],
|
||||||
"time": null
|
"time": 12.26
|
||||||
},
|
},
|
||||||
"part2": {
|
"part2": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "1168440",
|
||||||
"attempts": [],
|
"attempts": [],
|
||||||
"time": null
|
"time": 22.72
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
12
README.md
12
README.md
|
@ -19,7 +19,7 @@
|
||||||
[](src/day06)
|
[](src/day06)
|
||||||
[](src/day07)
|
[](src/day07)
|
||||||
[](src/day08)
|
[](src/day08)
|
||||||

|
[](src/day09)
|
||||||

|

|
||||||

|

|
||||||

|

|
||||||
|
@ -125,9 +125,9 @@ Both parts: 19.64ms
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 09
|
Day 09
|
||||||
Time part 1: -
|
Time part 1: 12.26ms
|
||||||
Time part 2: -
|
Time part 2: 22.72ms
|
||||||
Both parts: -
|
Both parts: 34.98ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -243,8 +243,8 @@ Both parts: -
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Total stars: 16/50
|
Total stars: 18/50
|
||||||
Total time: 398.31999999999994ms
|
Total time: 433.29999999999995ms
|
||||||
```
|
```
|
||||||
|
|
||||||
<!--/RESULTS-->
|
<!--/RESULTS-->
|
||||||
|
|
9
src/day09/README.md
Normal file
9
src/day09/README.md
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# 🎄 Advent of Code 2021 - day 9 🎄
|
||||||
|
|
||||||
|
## Info
|
||||||
|
|
||||||
|
Task description: [link](https://adventofcode.com/2021/day/9)
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
...
|
83
src/day09/index.js
Normal file
83
src/day09/index.js
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
import run from "aocrunner"
|
||||||
|
|
||||||
|
const parseInput = (rawInput) =>
|
||||||
|
rawInput.trim().split`\n`.map((y) => [...y].map((x) => +x))
|
||||||
|
|
||||||
|
function risk(x, y, map) {
|
||||||
|
let base = map[y][x]
|
||||||
|
if (map[y - 1] && map[y - 1][x] <= base) return undefined
|
||||||
|
if (map[y + 1] && map[y + 1][x] <= base) return undefined
|
||||||
|
if (map[y][x - 1] <= base) return undefined
|
||||||
|
if (map[y][x + 1] <= base) return undefined
|
||||||
|
return base
|
||||||
|
}
|
||||||
|
|
||||||
|
const part1 = (rawInput) => {
|
||||||
|
const input = parseInput(rawInput)
|
||||||
|
let lowSum = 0
|
||||||
|
for (const y in input) {
|
||||||
|
for (const x in input[y]) {
|
||||||
|
const r = risk(+x, +y, input)
|
||||||
|
lowSum += r + 1 || 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lowSum
|
||||||
|
}
|
||||||
|
|
||||||
|
const part2 = (rawInput) => {
|
||||||
|
const input = parseInput(rawInput)
|
||||||
|
const basinSizes = []
|
||||||
|
for (const Y in input) {
|
||||||
|
for (const X in input[Y]) {
|
||||||
|
const x = +X,
|
||||||
|
y = +Y
|
||||||
|
const r = risk(x, y, input)
|
||||||
|
if (r !== undefined) {
|
||||||
|
const basin = []
|
||||||
|
function spread(y, x) {
|
||||||
|
const key = `${y},${x}`
|
||||||
|
if (input[y][x] === 9 || basin.includes(key)) return
|
||||||
|
basin.push(key)
|
||||||
|
if (input[y - 1]) spread(y - 1, x)
|
||||||
|
if (input[y + 1]) spread(y + 1, x)
|
||||||
|
if (input[y][x - 1] !== undefined) spread(y, x - 1)
|
||||||
|
if (input[y][x + 1] !== undefined) spread(y, x + 1)
|
||||||
|
}
|
||||||
|
spread(y, x)
|
||||||
|
basinSizes.push(basin.length)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
basinSizes.sort((a, b) => b - a)
|
||||||
|
return basinSizes[0] * basinSizes[1] * basinSizes[2]
|
||||||
|
}
|
||||||
|
|
||||||
|
run({
|
||||||
|
part1: {
|
||||||
|
tests: [
|
||||||
|
{
|
||||||
|
input: `2199943210
|
||||||
|
3987894921
|
||||||
|
9856789892
|
||||||
|
8767896789
|
||||||
|
9899965678`,
|
||||||
|
expected: 15,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
solution: part1,
|
||||||
|
},
|
||||||
|
part2: {
|
||||||
|
tests: [
|
||||||
|
{
|
||||||
|
input: `2199943210
|
||||||
|
3987894921
|
||||||
|
9856789892
|
||||||
|
8767896789
|
||||||
|
9899965678`,
|
||||||
|
expected: 1134,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
solution: part2,
|
||||||
|
},
|
||||||
|
trimTestInputs: true,
|
||||||
|
})
|
Loading…
Add table
Add a link
Reference in a new issue