2020 day 3 - simple 2d grid traversal

This commit is contained in:
alexchao26
2020-12-03 00:28:22 -05:00
parent 79925f6507
commit ce80ce9f81
5 changed files with 448 additions and 9 deletions
+323
View File
@@ -0,0 +1,323 @@
.#.......#...........#.........
..##.......#.#.#.....##...#....
.......#..#.....#...#..........
...#..........###...#........##
#.#..#.#.##.#........#.#.....#.
#..#....#..#....#..............
#..#........#..................
..#.#...#.#...#....#.#.#..#....
..............#..#.............
.##....#...................#...
........#..........#......#...#
.##..#..#...##..........#...#..
.#...#....#.........#...#.....#
.#........##............#.#....
...........#..............##...
.#..#......#..#..............#.
..#.#.#...........#........#...
..###..........#....#.#......#.
.......#...##..........#.......
........#...#..................
....#....#..#.......#........#.
.......##.#......#.....#...##..
..#.#........................#.
.#.....#.##..............#.#...
..#.#...#.#..#....#....#.......
.#....##.....#....#........#...
..#...........#.##....#...#....
..#.##...#....#.#.....##...#...
.......#...####...#...#.......#
.#...#.........................
.......................#.......
.....#.#.........#..........#.#
#.........#............###..#..
.....#.#.............###.......
...#..#........#.#.......#.....
...................#....#......
...#..#...#............#..##...
...#.....#....#.......##......#
.....#....#...##..#..#...#...#.
..........#...........#.#.#....
..#.......#...#.....#......#...
.........#.......##......#..#.#
..#.....#..#.###...#.#......#..
#....#...#..#...#.....#........
..#......#..#.......#.#.....#..
#......#...#......#.....##.#...
........##.......#.......#.....
.#.#...............#...........
..............#...#.#....#.....
....#......#.#..#......#.......
...##....#....#...#............
.#...............#...........#.
.#.#...#.#.....#.....#...#.#...
...##...........#.....#..#...#.
.#.#...##.#.#......#......#....
.##.....#.......##....#.#.#....
.......#...........#....#....#.
....#...........#......#.####..
......#....#...#...##.......#..
......................#.#####..
..#...#.#...#..#..#......#.....
....#........##.......##....#..
#.#......##.........##.#..#...#
.#.#....#...#..#...#...##....#.
.....##...#....#....#.#........
......#..#....#.#...#..........
.........#...................#.
............#.###....#.#.......
...#.#.....#......#....#.#..#..
..............#..#.#.#.#.......
#..##...................##.....
..#.......#..#.........##..#...
.........##...#......#........#
..#.........#........##.###.#..
...........#.#....#.....###....
..#....##.#..#.##....#.....##..
..#.....#.##..................#
#....#.........................
..............#..#...#.#.......
......#..#.#.##....#..........#
..#.........#.####.....#.......
......#..#.#..........#...#....
......#.................#..#.#.
.....#..........#..............
....#.....#............#....##.
.....#.....#........#..........
............#.....#...#........
........#....#.#...............
#.....#.........#......#..#.#..
...#..#......#......#.......#..
.....#......#.#....#..#...#...#
......................#..##....
.............#.........###....#
#..............#.#..........##.
...#.#.................##......
...........#.#.....#...........
.........#.................#.#.
........#........#...#..##...#.
........#......##.......###....
..............#.#.#............
.#.....###...##.#......#.....#.
.............#......#.#.#...#.#
..#.........#.......#.....#....
......#........#...##......#...
.##..........##......#.#.....#.
..#.##....#....#...............
......#...#..#.....#.....#...#.
.......##..##..#............##.
..............#...##........#..
#....#................#..#.....
........#.......#.#.#...#......
......#.......#..............#.
#.#..#...#........#....#..####.
..#........#...........#.....#.
.##...........................#
.............#...........#.....
.#.....#.#...#.........#.......
..........#...#....#....#......
.#..#........##....#...........
.......###......##...#.........
..........#.#.#..#.#....#......
........##..#.........#....#...
........#.#......#.#...#.#..#..
....#....................#.##..
##....#..#...........#.....#.#.
...#..............##...##..#.#.
......#.##.#.......#..#...#....
....#..#..##.....#.....#.#....#
.......#....##.##..............
#..##....#.....#.#.............
..................#......#..#..
..#......#...#..#.......#...#..
...........#....#.#.....#......
#..#...##.........###..#......#
.......#......................#
#.......#....................#.
..#..#..........#..#..#....#...
.##..#..#.....#.#..##..........
#..###.......#..##..#...#..#.#.
.....##......###.....#.#.##...#
..............#...#....#.#.....
#...........#..................
..............#....#..##..#..#.
.........#.............#.......
.#.#....#....#...............##
.##.##.#.....###.....#.........
....#..............##......#...
....#........##................
....#.....#....#....##....##...
.#........#......#......#......
....#..........#...............
##..........#......#.....#.....
........#.#..#.#..#.....##.....
..##......#.#.......#.#..#.....
.#.......#......#...........#..
..#.#..#.#..................#..
...#...#...#...##......#.......
.#...##....#...#...#...#.......
.......#.#.......#.............
.#.##.#.....#...........#.##.#.
.#.##.#........#...##..........
.#.....#.....#....#..#.........
...##.............##...........
.#........##.....#.......#...#.
...........#..#..##........##..
.....#..#......................
..#.......#....................
.....#......#....#....#.......#
........#..#.#.....#......#....
..........#..#.....#......#....
..........#####.....#........#.
........#..#...#.#....#......#.
.........#...#....#.#..........
......#....##..........#...#...
#..............###.#.#.........
.#.#............##......#.#..#.
......#........................
...#..#......#.......#....#...#
.......#....##.....#.#......##.
...........#..........#..#.....
...........#..#.....###......#.
.......#....#..##......#.......
.........#.#.#.......#..#...#..
.......#.......##.....##...#...
..............#....#.....#.....
...#....#.....#.#..........##..
###.........#.............#....
...##......#.#........#....#..#
#....###.......#...#.#......##.
....#...##.......#......#.....#
.....#......#..................
#........##....#....#.#........
........#.......###...#........
........#..#.......###.........
..............#......#..#......
#......#.....#....#.#..........
.#......##.#.#.....#...#.#....#
.##...........#..#.##.....#....
.....#.....................#...
.#..#...#...##.#...#...........
.......#.......##..#.#..#......
.......##.....#.....#..........
.................#.............
#........#..#.......##.........
#...#..###.#..#....#.#.###.....
..#.......#.......#.......#....
..............#............##..
.#...#..#...##.........#....#..
#...........#...#..............
.......#.....#......#..#.....#.
..........#......#.............
##.........###..##.#....#..#.#.
..............###..............
#..##.............##.....#.....
....##...................#..#..
....#.....#..............#..#.#
........#........##...#.....##.
#...........#.##..........##...
#......##.....#...............#
..##..#....#.................#.
#.......##.....................
...............#.##..##......#.
..#.##..#.#....#.......##......
......##....#............##....
.#..#..##.....#.##....#........
#.........#..........#...#....#
...#.......#.............#.#.#.
..##............#...##........#
.......#.#.#........#..........
.....#.............#.....#.....
.........#.........#.........#.
#.....#....#.......#...........
.........#....#.............#.#
.##..#.......#...#......#......
....#....#....#........#....#..
............#.......#..#......#
.#............#.##........##...
..#...##...#....#...#.#...#..#.
#...#..........#..##.........#.
..#.........................#.#
...........#.........#..#.##...
.#..................#..#.......
......#......#...........#..#..
...##.....#.....#..#.......#...
.........#.#.......#......#....
...........#................#..
.....#...#..#............##....
.#.......#..#....#..........#..
#.....#..#.....#..##.......##..
...#.......#...#....#...#.#..##
...#...##......#....#....#.....
.......###.#..#.......#......#.
........#.#...#..#..#...#....#.
....#.........##.#.....#.......
....#.........#..##........#...
..#...........#......#....#.##.
.....................#.........
...................##......#..#
......#.#.....##..##..........#
..#.##........#.#.#..........#.
.#.......#...##.#....#....#....
#.#......#..#..#.......#.......
.............#........#.......#
....#...#.....#........#...#...
..#..............##..#.........
..#.................#..#...##..
....#..#...#...................
......#.........##.#..#..#...##
........#..#....#.......#.#.##.
.#...#...........#..........#..
##.....#...#............##...#.
.##.....#...#..................
.#.......####.#..##.##.#......#
.............#...#..#..#.......
...#.##.........#.#....#.......
...........##...##....#....##..
........#......#...#...........
...........#..#...#....#.##....
..##....#..........#....#...#..
#....#.#.#.......#.#...........
......#............##..........
#.#.###..#....#.......#...#....
.#......##..#..#.#.........#..#
..#.........#........#....#....
......##.#.......##....#..#..##
.............#...#............#
......#......#...#.#.#.##.#....
#.#...#.##.....#..............#
..........#.............##.##..
#......#....#...#.#.#.#..#....#
........#........#...#.#......#
.....#...........#.............
...........#....#..........#...
....####...#..##....#.#........
.#......#...#..#...........#...
#......###..#.##.###...........
..#...........#.........#....#.
................#.#....#..#.##.
...................#......#....
....#.#.....#.......#...###.##.
.#........#.#....#...#..#...#..
....#..###.................#..#
.....#.#..#........#......#..#.
....#.....#...............#...#
............##.#.........#..#..
.......#..#..##.#.#...##.......
..#..........##..#..#........#.
..............#..#...#.........
......#.#....#........##.......
.#.....##....#..#...#.......##.
..............#.##.............
#..#..#...##....##.#.....##.#..
..#...###..#.........##........
........##......#.....#..###...
.....#......##.###.............
....#.....#.#..#.#..#..........
....#..#.......#...........#...
.#.............#..#......##....
..#.#......#.#.................
.......#.#.#............#..#...
......###....##............#..#
.........#....#......#.........
..........#...............#..#.
+59 -2
View File
@@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/util"
)
@@ -15,17 +16,73 @@ func main() {
if part == 1 {
ans := part1(util.ReadFile("./input.txt"))
util.CopyToClipboard(fmt.Sprintf("%v", ans))
fmt.Println("Output:", ans)
} else {
ans := part2(util.ReadFile("./input.txt"))
util.CopyToClipboard(fmt.Sprintf("%v", ans))
fmt.Println("Output:", ans)
}
}
func part1(input string) int {
return 0
grid := parseInput(input)
return rideSlopes(grid, 3, 1)
}
func part2(input string) int {
return 0
grid := parseInput(input)
slopes := [][2]int{
// right, down
{1, 1},
{3, 1},
{5, 1},
{7, 1},
{1, 2},
}
ans := 1
for _, slope := range slopes {
ans *= rideSlopes(grid, slope[0], slope[1])
}
return ans
}
func parseInput(input string) (grid [][]bool) {
lines := strings.Split(input, "\n")
grid = make([][]bool, len(lines))
for i, l := range lines {
grid[i] = make([]bool, len(l))
for j, v := range l {
if v == '#' {
grid[i][j] = true
}
}
}
return grid
}
func rideSlopes(grid [][]bool, right, down int) int {
var row, col int
var ans int
for {
row += down
col += right
col %= len(grid[0])
if row < len(grid) {
if grid[row][col] {
ans++
}
} else {
break
}
}
return ans
}
+31 -7
View File
@@ -1,19 +1,33 @@
package main
import "testing"
import (
"testing"
"github.com/alexchao26/advent-of-code-go/util"
)
var tests1 = []struct {
name string
want int
input string
// add extra args if needed
}{
// {"actual", ACTUAL_ANSWER, util.ReadFile("input.txt")},
{"example", 7, `..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#`},
{"actual", 209, util.ReadFile("input.txt")},
}
func TestPart1(t *testing.T) {
for _, test := range tests1 {
t.Run(test.name, func(*testing.T) {
t.Run(test.name, func(t *testing.T) {
got := part1(test.input)
if got != test.want {
t.Errorf("got %v, want %v", got, test.want)
@@ -26,14 +40,24 @@ var tests2 = []struct {
name string
want int
input string
// add extra args if needed
}{
// {"actual", ACTUAL_ANSWER, util.ReadFile("input.txt")},
{"example", 336, `..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#`},
{"actual", 1574890240, util.ReadFile("input.txt")},
}
func TestPart2(t *testing.T) {
for _, test := range tests2 {
t.Run(test.name, func(*testing.T) {
t.Run(test.name, func(t *testing.T) {
got := part2(test.input)
if got != test.want {
t.Errorf("got %v, want %v", got, test.want)