main.go (1001B)
1 package main
2
3 import (
4 "bufio"
5 "fmt"
6 "os"
7 )
8
9 func readInput(filename string) (res []string) {
10 file, _ := os.Open(filename)
11 defer file.Close()
12 scanner := bufio.NewScanner(file)
13 for scanner.Scan() {
14 res = append(res, scanner.Text())
15 }
16 return
17 }
18
19 func main() {
20 input := readInput("./input.txt")
21 // 240
22 fmt.Println(part1(input))
23 // 2832009600
24 fmt.Println(part2(input))
25 }
26
27 func countTrees(input []string, dx int, dy int) (tree_count int) {
28 for x, y := dx, dy; y < len(input); x, y = (x+dx)%len(input[0]), y+dy {
29 if input[y][x] == '#' {
30 tree_count += 1
31 }
32 }
33 return
34 }
35
36 func part1(input []string) int {
37 return countTrees(input, 3, 1)
38 }
39
40 func part2(input []string) int {
41 // Right 1, down 1.
42 // Right 3, down 1. (This is the slope you already checked.)
43 // Right 5, down 1.
44 // Right 7, down 1.
45 // Right 1, down 2.
46 dxs := []int{1, 3, 5, 7, 1}
47 dys := []int{1, 1, 1, 1, 2}
48 tree_prod := 1
49 for i, dx := range dxs {
50 tree_prod *= countTrees(input, dx, dys[i])
51 }
52 return tree_prod
53 }