-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (62 loc) · 1.09 KB
/
main.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
package main
import (
"day3/lines"
"fmt"
"time"
)
type pos bool
const (
empty = false
tree = true
)
type slope [][]pos
func main() {
s := fileToSlope("data")
start := time.Now()
fmt.Println(part1(s))
fmt.Println(part2(s))
t := time.Now()
fmt.Println(t.Sub(start))
}
func part1(s slope) int {
return s.countTrees(3, 1)
}
func part2(s slope) int {
c1 := s.countTrees(1, 1)
c2 := s.countTrees(3, 1)
c3 := s.countTrees(5, 1)
c4 := s.countTrees(7, 1)
c5 := s.countTrees(1, 2)
return c1 * c2 * c3 * c4 * c5
}
func fileToSlope(filename string) slope {
lns := lines.MustParse(filename, "\n")
var s slope
for _, line := range lns {
var row []pos
for _, c := range line {
if c == '#' {
row = append(row, tree)
} else {
row = append(row, empty)
}
}
s = append(s, row)
}
return s
}
func (s slope) readPos(posx, posy int) pos {
posx %= len(s[0])
return s[posy][posx]
}
func (s slope) countTrees(dx, dy int) int {
posx := 0
count := 0
for posy := 0; posy < len(s); posy += dy {
if s.readPos(posx, posy) == tree {
count++
}
posx += dx
}
return count
}