advent-20/solutions/day03.go

45 lines
890 B
Go
Raw Normal View History

2024-02-25 21:37:21 +00:00
package solutions
import "strings"
2024-02-25 21:43:55 +00:00
func countTrees(input string, right, down int) int {
2024-02-25 21:37:21 +00:00
verticalPos := down
2024-02-25 21:43:55 +00:00
horizontalPos := right
2024-02-25 21:37:21 +00:00
lines := strings.Split(input, "\n")
width := len(lines[0])
treesCount := 0
for verticalPos < len(lines) {
line := lines[verticalPos]
charAt := string(line[horizontalPos])
if charAt == "#" {
treesCount += 1
}
2024-02-25 21:43:55 +00:00
horizontalPos += right
2024-02-25 21:37:21 +00:00
horizontalPos = horizontalPos % width
verticalPos += down
}
return treesCount
}
func Day03Part01(isTest bool) int {
input := ReadInput("03", isTest)
return countTrees(input, 3, 1)
}
func Day03Part02(isTest bool) int {
2024-02-25 21:43:55 +00:00
input := ReadInput("03", isTest)
amount1 := countTrees(input, 1, 1)
amount2 := countTrees(input, 3, 1)
amount3 := countTrees(input, 5, 1)
amount4 := countTrees(input, 7, 1)
amount5 := countTrees(input, 1, 2)
return amount1 * amount2 * amount3 * amount4 * amount5
2024-02-25 21:37:21 +00:00
}