-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.go
75 lines (60 loc) · 1.21 KB
/
solver.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
package day03_2020
import (
"github.com/andrerfcsantos/Advent-Of-Code/2020/go/puzzle/utils"
"strconv"
)
type Solver struct {
Board [][]bool
Depth int
Width int
}
func NewSolver() *Solver {
return &Solver{}
}
func (s *Solver) ProcessInput(input string) error {
lines := utils.TrimmedLinesNoEmpty(input)
for _, line := range lines {
var l []bool
for _, c := range line {
l = append(l, c == '#')
}
s.Board = append(s.Board, l)
}
s.Width = len(s.Board[0])
s.Depth = len(s.Board)
return nil
}
func (s *Solver) Part1() (string, error) {
trees := s.TreesWithSlope(utils.Point2D{X: 3, Y: 1})
return strconv.Itoa(trees), nil
}
func (s *Solver) Part2() (string, error) {
slopes := []utils.Point2D{
{X: 1, Y: 1},
{X: 3, Y: 1},
{X: 5, Y: 1},
{X: 7, Y: 1},
{X: 1, Y: 2},
}
var trees []int
for _, slope := range slopes {
t := s.TreesWithSlope(slope)
trees = append(trees, t)
}
res := 1
for _, t := range trees {
res *= t
}
return strconv.Itoa(res), nil
}
func (s *Solver) TreesWithSlope(slope utils.Point2D) int {
treeCount := 0
pos := utils.Point2D{X: 0, Y: 0}
for pos.Y < s.Depth {
if s.Board[pos.Y][pos.X%s.Width] {
treeCount++
}
pos.Add(slope)
}
return treeCount
}