diff --git a/day08/common/forest.go b/day08/common/forest.go new file mode 100644 index 0000000..bf4f42d --- /dev/null +++ b/day08/common/forest.go @@ -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 +} diff --git a/day08/ex1/main.go b/day08/ex1/main.go new file mode 100644 index 0000000..ed3c5ef --- /dev/null +++ b/day08/ex1/main.go @@ -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) +} diff --git a/day08/ex2/main.go b/day08/ex2/main.go new file mode 100644 index 0000000..07c5b0f --- /dev/null +++ b/day08/ex2/main.go @@ -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) +}