mirror of
https://github.com/seigler/aoc2022
synced 2025-07-26 06:26:09 +00:00
day 8
This commit is contained in:
parent
429e4c64eb
commit
fed2b6ec18
4 changed files with 127 additions and 12 deletions
|
@ -103,16 +103,16 @@
|
|||
},
|
||||
{
|
||||
"part1": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "1679",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 8.862513
|
||||
},
|
||||
"part2": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "536625",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 8.312961
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
12
README.md
12
README.md
|
@ -18,7 +18,7 @@
|
|||
[](src/day05)
|
||||
[](src/day06)
|
||||
[](src/day07)
|
||||

|
||||
[](src/day08)
|
||||

|
||||

|
||||

|
||||
|
@ -118,9 +118,9 @@ Both parts: 2.9718489999999997ms
|
|||
|
||||
```
|
||||
Day 08
|
||||
Time part 1: -
|
||||
Time part 2: -
|
||||
Both parts: -
|
||||
Time part 1: 7.541ms
|
||||
Time part 2: 8.005ms
|
||||
Both parts: 15.545933000000002ms
|
||||
```
|
||||
|
||||
```
|
||||
|
@ -243,8 +243,8 @@ Both parts: -
|
|||
```
|
||||
|
||||
```
|
||||
Total stars: 14/50
|
||||
Total time: 23.909ms
|
||||
Total stars: 16/50
|
||||
Total time: 39.454ms
|
||||
```
|
||||
|
||||
<!--/RESULTS-->
|
||||
|
|
9
src/day08/README.md
Normal file
9
src/day08/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2022 - day 8 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2022/day/8)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
106
src/day08/index.ts
Normal file
106
src/day08/index.ts
Normal file
|
@ -0,0 +1,106 @@
|
|||
import run from "aocrunner"
|
||||
import { rmdir } from "fs"
|
||||
|
||||
const parseInput = (rawInput: string) =>
|
||||
rawInput.split("\n").map((row) => [...row].map((x) => +x))
|
||||
|
||||
const cardinalDirections = [
|
||||
[-1, 0],
|
||||
[1, 0],
|
||||
[0, -1],
|
||||
[0, 1],
|
||||
]
|
||||
|
||||
const part1 = (rawInput: string) => {
|
||||
const forest = parseInput(rawInput)
|
||||
const width = forest[0].length
|
||||
const depth = forest.length
|
||||
let totalVisible = 0
|
||||
//for each tree:
|
||||
for (let row = 0; row < depth; row++) {
|
||||
for (let col = 0; col < width; col++) {
|
||||
const thisHeight = forest[row][col]
|
||||
findAnyVisibility: {
|
||||
// labelled block
|
||||
for (const [dr, dc] of cardinalDirections) {
|
||||
let r = row + dr,
|
||||
c = col + dc
|
||||
findThisVisibility: {
|
||||
while (r >= 0 && r < depth && c >= 0 && c < width) {
|
||||
if (forest[r][c] >= thisHeight) {
|
||||
break findThisVisibility // we are blocked this direction
|
||||
}
|
||||
r += dr
|
||||
c += dc
|
||||
}
|
||||
totalVisible++
|
||||
break findAnyVisibility
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return totalVisible
|
||||
}
|
||||
|
||||
const part2 = (rawInput: string) => {
|
||||
const forest = parseInput(rawInput)
|
||||
const width = forest[0].length
|
||||
const depth = forest.length
|
||||
let bestScore = 0
|
||||
//for each tree:
|
||||
for (let row = 0; row < depth; row++) {
|
||||
for (let col = 0; col < width; col++) {
|
||||
const thisHeight = forest[row][col]
|
||||
let score = 1
|
||||
for (const [dr, dc] of cardinalDirections) {
|
||||
let r = row + dr,
|
||||
c = col + dc,
|
||||
d = 0
|
||||
while (r >= 0 && r < depth && c >= 0 && c < width) {
|
||||
if (forest[r][c] >= thisHeight) {
|
||||
d++
|
||||
break // we are blocked this direction
|
||||
}
|
||||
r += dr
|
||||
c += dc
|
||||
d++
|
||||
}
|
||||
score *= d
|
||||
}
|
||||
bestScore = Math.max(bestScore, score)
|
||||
}
|
||||
}
|
||||
return bestScore
|
||||
}
|
||||
|
||||
run({
|
||||
part1: {
|
||||
tests: [
|
||||
{
|
||||
input: `30373
|
||||
25512
|
||||
65332
|
||||
33549
|
||||
35390`,
|
||||
expected: 21,
|
||||
},
|
||||
],
|
||||
solution: part1,
|
||||
},
|
||||
part2: {
|
||||
tests: [
|
||||
{
|
||||
input: `30373
|
||||
25512
|
||||
65332
|
||||
33549
|
||||
35390`,
|
||||
expected: 8,
|
||||
},
|
||||
],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
onlyTests: false,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue