Day 8
This commit is contained in:
25
day08/common/forest.go
Normal file
25
day08/common/forest.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Tree int
|
||||||
|
|
||||||
|
type Forest [][]Tree
|
||||||
|
|
||||||
|
|
||||||
|
func Parse(scanner bufio.Scanner) Forest {
|
||||||
|
forest := Forest{}
|
||||||
|
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
forestRow := make([]Tree, len(line))
|
||||||
|
for i, character := range line {
|
||||||
|
forestRow[i] = Tree(character - '0')
|
||||||
|
}
|
||||||
|
forest = append(forest, forestRow)
|
||||||
|
}
|
||||||
|
|
||||||
|
return forest
|
||||||
|
}
|
||||||
60
day08/ex1/main.go
Normal file
60
day08/ex1/main.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"aoc2022/day08/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
forest := common.Parse(*bufio.NewScanner(os.Stdin))
|
||||||
|
nVisibleTrees := 0
|
||||||
|
|
||||||
|
for i := 0; i < len(forest); i++ {
|
||||||
|
for j := 0; j < len(forest[i]); j++ {
|
||||||
|
visible := true
|
||||||
|
for k := 0; k < i; k++ {
|
||||||
|
if forest[k][j] >= forest[i][j] {
|
||||||
|
visible = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if visible {
|
||||||
|
nVisibleTrees++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
visible = true
|
||||||
|
for k := 0; k < j; k++ {
|
||||||
|
if forest[i][k] >= forest[i][j] {
|
||||||
|
visible = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if visible {
|
||||||
|
nVisibleTrees++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
visible = true
|
||||||
|
for k := i + 1; k < len(forest); k++ {
|
||||||
|
if forest[k][j] >= forest[i][j] {
|
||||||
|
visible = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if visible {
|
||||||
|
nVisibleTrees++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
visible = true
|
||||||
|
for k := j + 1; k < len(forest[i]); k++ {
|
||||||
|
if forest[i][k] >= forest[i][j] {
|
||||||
|
visible = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if visible {
|
||||||
|
nVisibleTrees++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(nVisibleTrees)
|
||||||
|
}
|
||||||
61
day08/ex2/main.go
Normal file
61
day08/ex2/main.go
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"aoc2022/day08/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
forest := common.Parse(*bufio.NewScanner(os.Stdin))
|
||||||
|
maxScore := 0
|
||||||
|
|
||||||
|
for i := 0; i < len(forest); i++ {
|
||||||
|
for j := 0; j < len(forest[i]); j++ {
|
||||||
|
score := 1
|
||||||
|
|
||||||
|
currScore := 0
|
||||||
|
for k := i - 1; k >= 0; k-- {
|
||||||
|
currScore++
|
||||||
|
if forest[k][j] >= forest[i][j] {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
score *= currScore
|
||||||
|
|
||||||
|
currScore = 0
|
||||||
|
for k := j - 1; k >= 0; k-- {
|
||||||
|
currScore++
|
||||||
|
if forest[i][k] >= forest[i][j] {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
score *= currScore
|
||||||
|
|
||||||
|
currScore = 0
|
||||||
|
for k := i + 1; k < len(forest); k++ {
|
||||||
|
currScore++
|
||||||
|
if forest[k][j] >= forest[i][j] {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
score *= currScore
|
||||||
|
|
||||||
|
currScore = 0
|
||||||
|
for k := j + 1; k < len(forest[i]); k++ {
|
||||||
|
currScore++
|
||||||
|
if forest[i][k] >= forest[i][j] {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
score *= currScore
|
||||||
|
|
||||||
|
if score > maxScore {
|
||||||
|
maxScore = score
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(maxScore)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user