-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.go
82 lines (61 loc) · 1.48 KB
/
day03.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package day03
import (
"strconv"
"strings"
)
type Day03 struct{}
func (d *Day03) InputFileName() string { return "input" }
func (d *Day03) PartOne(input string) string {
tMap := buildMap(input)
return strconv.Itoa(countTrees(tMap, 3, 1))
}
func (d *Day03) PartTwo(input string) string {
tMap := buildMap(input)
treesSlopes := make([]int, len(slopes))
for i, slopeData := range slopes {
stepRight, stepDown := slopeData[0], slopeData[1]
treesSlopes[i] = countTrees(tMap, stepRight, stepDown)
}
result := 1
for _, trees := range treesSlopes {
result = result * trees
}
return strconv.Itoa(result)
}
func buildMap(input string) [][]rune {
rows := strings.Split(input, "\n")
tMap := make([][]rune, len(rows))
for rowIdx, row := range rows {
tMap[rowIdx] = []rune(row)
}
return tMap
}
func step(tmap [][]rune, rowIdx int, colIdx int, stepRight int, stepDown int) (newRowIdx int, newColIdx int) {
rowLen := len(tmap[0])
newColIdx = colIdx + stepRight
newRowIdx = rowIdx + stepDown
if newColIdx >= rowLen {
newColIdx = newColIdx - rowLen
}
return newRowIdx, newColIdx
}
func countTrees(tMap [][]rune, stepsRight int, stepsDown int) int {
trees := 0
rowIdx := 0
colIdx := 0
for rowIdx < len(tMap)-1 {
rowIdx, colIdx = step(tMap, rowIdx, colIdx, stepsRight, stepsDown)
gridObj := string(tMap[rowIdx][colIdx])
if gridObj == "#" {
trees++
}
}
return trees
}
var slopes [][]int = [][]int{
[]int{1, 1},
[]int{3, 1},
[]int{5, 1},
[]int{7, 1},
[]int{1, 2},
}