-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (55 loc) · 1.18 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
// Package day03 - Solution for Advent of Code 2020/03
// Problem Link: http://adventofcode.com/2020/day/03
package day03
import (
_ "embed"
"fmt"
"strings"
)
//go:embed input.txt
var input string
// Run prints out the result of the solution.
func Run() {
fmt.Println(solve(input))
}
func solve(input string) (int, int) {
coords := parse(input)
return solvePart1(coords), solvePart2(coords)
}
func solvePart1(coords [][]string) (trees int) {
return countTrees(coords, 3, 1)
}
func solvePart2(coords [][]string) int {
slopes := [][]int{
{1, 1},
{3, 1},
{5, 1},
{7, 1},
{1, 2},
}
result := 1
for _, rightDown := range slopes {
result *= countTrees(coords, rightDown[0], rightDown[1])
}
return result
}
func countTrees(coords [][]string, right, down int) (trees int) {
horizontal, vertical := 0, 0
for {
if coords[horizontal][vertical] == "#" {
trees++
}
horizontal += down
vertical = (vertical + right) % len(coords[0])
if horizontal >= len(coords) {
break
}
}
return trees
}
func parse(input string) (coords [][]string) {
for _, lines := range strings.Split(input, "\n") {
coords = append(coords, strings.Split(lines, ""))
}
return coords
}