This commit is contained in:
cupcakearmy 2020-12-04 23:56:25 +01:00
parent 6011cf2b4e
commit b03c50fcfb
2 changed files with 67 additions and 1 deletions

66
solutions/3/go/main.go Normal file
View File

@ -0,0 +1,66 @@
package main
import (
"fmt"
"io/ioutil"
"strings"
)
const high = rune('#')
type sForest struct {
data [][]bool
height int
width int
}
func (f sForest) at(y, x int) bool {
if y >= f.height {
return false
}
return f.data[y][x%f.width]
}
func (f sForest) traverse(y, x int) int {
trees := 0
for dy := 0; dy <= f.height; dy++ {
tree := f.at(dy*y, dy*x)
if tree {
trees++
}
}
return trees
}
func main() {
data, _ := ioutil.ReadFile("./solutions/3/data.txt")
rows := strings.Split(strings.TrimSpace(string(data)), "\n")
height, width := len(rows), len(rows[0])
d := make([][]bool, height)
for y, row := range rows {
d[y] = make([]bool, width)
for x, char := range []rune(row) {
d[y][x] = char == high
}
}
forest := sForest{
data: d,
height: height,
width: width,
}
fmt.Println("Simple: ", forest.traverse(1, 3))
trees11 := forest.traverse(1, 1)
trees13 := forest.traverse(1, 3)
trees15 := forest.traverse(1, 5)
trees17 := forest.traverse(1, 7)
trees21 := forest.traverse(2, 1)
fmt.Println(trees11, trees13, trees15, trees17, trees21)
fmt.Println(trees11 * trees13 * trees15 * trees17 * trees21)
}

View File

@ -19,7 +19,7 @@ class Forest():
return row[x % len(row)] == '#'
data = join(dirname(__file__), 'data.txt')
data = join(dirname(__file__), '../data.txt')
with open(data) as f:
forest = Forest(f.read())