adventofcode/day3/three-2.go

59 lines
1.0 KiB
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")
paths := [][]int{
{1, 1}, // {right,down}
{3, 1},
{5, 1},
{7, 1},
{1, 2},
}
total := 1 // this will have all the paths multiplied into it
for _, p := range paths {
x := 0
y := 0 // start in the top-left corner
trees := 0
for {
t := getCoords(lines, x, y)
done := false
switch t {
case 0:
done = true
case '#':
trees += 1
}
if done == true {
break
}
//fmt.Printf("in3.txt:%d:%d: %#v\n", y+1, (x%len(lines[y]))+1, tree)
x += p[0]
y += p[1]
}
total = total * trees
}
fmt.Println(total)
}