-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.go
45 lines (39 loc) · 1.05 KB
/
day3.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
package main
import (
"fmt"
"io/ioutil"
"strings"
)
func main() {
i, _ := ioutil.ReadFile("input.txt")
input := string(i)
fmt.Printf("Part 1: %v\n", SolveDay3Part1(listToSlice(input), 3, 1))
fmt.Printf("Part 2: %v\n", SolveDay3Part2(listToSlice(input)))
}
//SolveDay3Part1 count the trees that hit by a path
func SolveDay3Part1(trees []string, right int, down int) (sum int) {
i := 0
for in, p := range trees {
if (in % down) == 0 {
if len(p)-1 < i {
i = i - len(p)
}
if p[i] == '#' {
sum++
}
i = i + right
}
}
return
}
//SolveDay3Part2 count the trees that hit for 5 path and multiply them
func SolveDay3Part2(trees []string) (sum int) {
return SolveDay3Part1(trees, 1, 1) * SolveDay3Part1(trees, 3, 1) * SolveDay3Part1(trees, 5, 1) * SolveDay3Part1(trees, 7, 1) * SolveDay3Part1(trees, 1, 2)
}
//listToSlice converts the list of numbers (each number one row) to a slice
func listToSlice(list string) (s []string) {
for _, line := range strings.Split(strings.TrimSuffix(list, "\n"), "\n") {
s = append(s, line)
}
return
}