mirror of
https://github.com/cupcakearmy/advent-of-code.git
synced 2024-12-22 08:06:25 +00:00
3 in go
This commit is contained in:
parent
6011cf2b4e
commit
b03c50fcfb
66
solutions/3/go/main.go
Normal file
66
solutions/3/go/main.go
Normal 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)
|
||||
|
||||
}
|
@ -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())
|
||||
|
Loading…
Reference in New Issue
Block a user