46 lines
749 B
Go
46 lines
749 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"strings"
|
|
)
|
|
|
|
// Returns 0 if we find the end of the path, '.' or '#' otherwise
|
|
func getCoords(s []string, x, y int) rune {
|
|
length := len(s)
|
|
if y+1 >= length {
|
|
return 0
|
|
}
|
|
width := len(s[y])
|
|
if y < 0 || x < 0 { // off the map = no tree
|
|
return '.'
|
|
}
|
|
return rune(s[y][x%width])
|
|
|
|
}
|
|
|
|
func main() {
|
|
d, _ := ioutil.ReadFile("in3.txt")
|
|
text := string(d)
|
|
lines := strings.Split(text, "\n")
|
|
trees := 0
|
|
x := 0
|
|
y := 0 // start in the top-left corner
|
|
for {
|
|
t := getCoords(lines, x, y)
|
|
//tree := false
|
|
switch t {
|
|
case 0:
|
|
fmt.Println(trees)
|
|
return
|
|
case '#':
|
|
trees += 1
|
|
//tree = true
|
|
}
|
|
//fmt.Printf("in3.txt:%d:%d: %#v\n", y+1, (x%len(lines[y]))+1, tree)
|
|
x += 3
|
|
y += 1
|
|
}
|
|
}
|