This commit is contained in:
Joshua Seigler 2022-12-08 12:27:36 -05:00
parent 429e4c64eb
commit fed2b6ec18
4 changed files with 127 additions and 12 deletions

View file

@ -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
}
},
{

View file

@ -18,7 +18,7 @@
[![Day](https://badgen.net/badge/05/%E2%98%85%E2%98%85/green)](src/day05)
[![Day](https://badgen.net/badge/06/%E2%98%85%E2%98%85/green)](src/day06)
[![Day](https://badgen.net/badge/07/%E2%98%85%E2%98%85/green)](src/day07)
![Day](https://badgen.net/badge/08/%E2%98%86%E2%98%86/gray)
[![Day](https://badgen.net/badge/08/%E2%98%85%E2%98%85/green)](src/day08)
![Day](https://badgen.net/badge/09/%E2%98%86%E2%98%86/gray)
![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)
@ -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
View 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
View 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,
})