File tree 5 files changed +420
-1
lines changed
5 files changed +420
-1
lines changed Original file line number Diff line number Diff line change
1
+ package day03
2
+
3
+ import "github.com/alokmenghrajani/adventofcode2020/utils"
4
+
5
+ func Part1 (input []string ) uint {
6
+ grid := utils .Grid (input )
7
+ return collisions (grid , 3 , 1 )
8
+ }
9
+
10
+ func Part2 (input []string ) uint {
11
+ grid := utils .Grid (input )
12
+ deltas := [][]int {
13
+ {1 , 1 },
14
+ {3 , 1 },
15
+ {5 , 1 },
16
+ {7 , 1 },
17
+ {1 , 2 },
18
+ }
19
+ r := uint (1 )
20
+ for _ , delta := range deltas {
21
+ r *= collisions (grid , delta [0 ], delta [1 ])
22
+ }
23
+ return r
24
+ }
25
+
26
+ func collisions (grid [][]byte , dx , dy int ) uint {
27
+ x := 0
28
+ y := 0
29
+ c := uint (0 )
30
+ cols := len (grid [0 ])
31
+ for {
32
+ x += dx
33
+ y += dy
34
+ if y >= len (grid ) {
35
+ return c
36
+ }
37
+ if grid [y ][x % cols ] == '#' {
38
+ c ++
39
+ }
40
+ }
41
+ }
Original file line number Diff line number Diff line change
1
+ package day03
2
+
3
+ import (
4
+ "github.com/alecthomas/assert"
5
+ "testing"
6
+ )
7
+
8
+ func TestPart1 (t * testing.T ) {
9
+ r := Part1 ([]string {
10
+ "..##......." ,
11
+ "#...#...#.." ,
12
+ ".#....#..#." ,
13
+ "..#.#...#.#" ,
14
+ ".#...##..#." ,
15
+ "..#.##....." ,
16
+ ".#.#.#....#" ,
17
+ ".#........#" ,
18
+ "#.##...#..." ,
19
+ "#...##....#" ,
20
+ ".#..#...#.#" ,
21
+ })
22
+ assert .Equal (t , uint (7 ), r )
23
+ }
24
+
25
+ func TestPart2 (t * testing.T ) {
26
+ r := Part2 ([]string {
27
+ "..##......." ,
28
+ "#...#...#.." ,
29
+ ".#....#..#." ,
30
+ "..#.#...#.#" ,
31
+ ".#...##..#." ,
32
+ "..#.##....." ,
33
+ ".#.#.#....#" ,
34
+ ".#........#" ,
35
+ "#.##...#..." ,
36
+ "#...##....#" ,
37
+ ".#..#...#.#" ,
38
+ })
39
+ assert .Equal (t , uint (336 ), r )
40
+ }
You can’t perform that action at this time.
0 commit comments