mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 12:45:10 +02:00
2020 day 3 - simple 2d grid traversal
This commit is contained in:
Executable
+323
@@ -0,0 +1,323 @@
|
|||||||
|
.#.......#...........#.........
|
||||||
|
..##.......#.#.#.....##...#....
|
||||||
|
.......#..#.....#...#..........
|
||||||
|
...#..........###...#........##
|
||||||
|
#.#..#.#.##.#........#.#.....#.
|
||||||
|
#..#....#..#....#..............
|
||||||
|
#..#........#..................
|
||||||
|
..#.#...#.#...#....#.#.#..#....
|
||||||
|
..............#..#.............
|
||||||
|
.##....#...................#...
|
||||||
|
........#..........#......#...#
|
||||||
|
.##..#..#...##..........#...#..
|
||||||
|
.#...#....#.........#...#.....#
|
||||||
|
.#........##............#.#....
|
||||||
|
...........#..............##...
|
||||||
|
.#..#......#..#..............#.
|
||||||
|
..#.#.#...........#........#...
|
||||||
|
..###..........#....#.#......#.
|
||||||
|
.......#...##..........#.......
|
||||||
|
........#...#..................
|
||||||
|
....#....#..#.......#........#.
|
||||||
|
.......##.#......#.....#...##..
|
||||||
|
..#.#........................#.
|
||||||
|
.#.....#.##..............#.#...
|
||||||
|
..#.#...#.#..#....#....#.......
|
||||||
|
.#....##.....#....#........#...
|
||||||
|
..#...........#.##....#...#....
|
||||||
|
..#.##...#....#.#.....##...#...
|
||||||
|
.......#...####...#...#.......#
|
||||||
|
.#...#.........................
|
||||||
|
.......................#.......
|
||||||
|
.....#.#.........#..........#.#
|
||||||
|
#.........#............###..#..
|
||||||
|
.....#.#.............###.......
|
||||||
|
...#..#........#.#.......#.....
|
||||||
|
...................#....#......
|
||||||
|
...#..#...#............#..##...
|
||||||
|
...#.....#....#.......##......#
|
||||||
|
.....#....#...##..#..#...#...#.
|
||||||
|
..........#...........#.#.#....
|
||||||
|
..#.......#...#.....#......#...
|
||||||
|
.........#.......##......#..#.#
|
||||||
|
..#.....#..#.###...#.#......#..
|
||||||
|
#....#...#..#...#.....#........
|
||||||
|
..#......#..#.......#.#.....#..
|
||||||
|
#......#...#......#.....##.#...
|
||||||
|
........##.......#.......#.....
|
||||||
|
.#.#...............#...........
|
||||||
|
..............#...#.#....#.....
|
||||||
|
....#......#.#..#......#.......
|
||||||
|
...##....#....#...#............
|
||||||
|
.#...............#...........#.
|
||||||
|
.#.#...#.#.....#.....#...#.#...
|
||||||
|
...##...........#.....#..#...#.
|
||||||
|
.#.#...##.#.#......#......#....
|
||||||
|
.##.....#.......##....#.#.#....
|
||||||
|
.......#...........#....#....#.
|
||||||
|
....#...........#......#.####..
|
||||||
|
......#....#...#...##.......#..
|
||||||
|
......................#.#####..
|
||||||
|
..#...#.#...#..#..#......#.....
|
||||||
|
....#........##.......##....#..
|
||||||
|
#.#......##.........##.#..#...#
|
||||||
|
.#.#....#...#..#...#...##....#.
|
||||||
|
.....##...#....#....#.#........
|
||||||
|
......#..#....#.#...#..........
|
||||||
|
.........#...................#.
|
||||||
|
............#.###....#.#.......
|
||||||
|
...#.#.....#......#....#.#..#..
|
||||||
|
..............#..#.#.#.#.......
|
||||||
|
#..##...................##.....
|
||||||
|
..#.......#..#.........##..#...
|
||||||
|
.........##...#......#........#
|
||||||
|
..#.........#........##.###.#..
|
||||||
|
...........#.#....#.....###....
|
||||||
|
..#....##.#..#.##....#.....##..
|
||||||
|
..#.....#.##..................#
|
||||||
|
#....#.........................
|
||||||
|
..............#..#...#.#.......
|
||||||
|
......#..#.#.##....#..........#
|
||||||
|
..#.........#.####.....#.......
|
||||||
|
......#..#.#..........#...#....
|
||||||
|
......#.................#..#.#.
|
||||||
|
.....#..........#..............
|
||||||
|
....#.....#............#....##.
|
||||||
|
.....#.....#........#..........
|
||||||
|
............#.....#...#........
|
||||||
|
........#....#.#...............
|
||||||
|
#.....#.........#......#..#.#..
|
||||||
|
...#..#......#......#.......#..
|
||||||
|
.....#......#.#....#..#...#...#
|
||||||
|
......................#..##....
|
||||||
|
.............#.........###....#
|
||||||
|
#..............#.#..........##.
|
||||||
|
...#.#.................##......
|
||||||
|
...........#.#.....#...........
|
||||||
|
.........#.................#.#.
|
||||||
|
........#........#...#..##...#.
|
||||||
|
........#......##.......###....
|
||||||
|
..............#.#.#............
|
||||||
|
.#.....###...##.#......#.....#.
|
||||||
|
.............#......#.#.#...#.#
|
||||||
|
..#.........#.......#.....#....
|
||||||
|
......#........#...##......#...
|
||||||
|
.##..........##......#.#.....#.
|
||||||
|
..#.##....#....#...............
|
||||||
|
......#...#..#.....#.....#...#.
|
||||||
|
.......##..##..#............##.
|
||||||
|
..............#...##........#..
|
||||||
|
#....#................#..#.....
|
||||||
|
........#.......#.#.#...#......
|
||||||
|
......#.......#..............#.
|
||||||
|
#.#..#...#........#....#..####.
|
||||||
|
..#........#...........#.....#.
|
||||||
|
.##...........................#
|
||||||
|
.............#...........#.....
|
||||||
|
.#.....#.#...#.........#.......
|
||||||
|
..........#...#....#....#......
|
||||||
|
.#..#........##....#...........
|
||||||
|
.......###......##...#.........
|
||||||
|
..........#.#.#..#.#....#......
|
||||||
|
........##..#.........#....#...
|
||||||
|
........#.#......#.#...#.#..#..
|
||||||
|
....#....................#.##..
|
||||||
|
##....#..#...........#.....#.#.
|
||||||
|
...#..............##...##..#.#.
|
||||||
|
......#.##.#.......#..#...#....
|
||||||
|
....#..#..##.....#.....#.#....#
|
||||||
|
.......#....##.##..............
|
||||||
|
#..##....#.....#.#.............
|
||||||
|
..................#......#..#..
|
||||||
|
..#......#...#..#.......#...#..
|
||||||
|
...........#....#.#.....#......
|
||||||
|
#..#...##.........###..#......#
|
||||||
|
.......#......................#
|
||||||
|
#.......#....................#.
|
||||||
|
..#..#..........#..#..#....#...
|
||||||
|
.##..#..#.....#.#..##..........
|
||||||
|
#..###.......#..##..#...#..#.#.
|
||||||
|
.....##......###.....#.#.##...#
|
||||||
|
..............#...#....#.#.....
|
||||||
|
#...........#..................
|
||||||
|
..............#....#..##..#..#.
|
||||||
|
.........#.............#.......
|
||||||
|
.#.#....#....#...............##
|
||||||
|
.##.##.#.....###.....#.........
|
||||||
|
....#..............##......#...
|
||||||
|
....#........##................
|
||||||
|
....#.....#....#....##....##...
|
||||||
|
.#........#......#......#......
|
||||||
|
....#..........#...............
|
||||||
|
##..........#......#.....#.....
|
||||||
|
........#.#..#.#..#.....##.....
|
||||||
|
..##......#.#.......#.#..#.....
|
||||||
|
.#.......#......#...........#..
|
||||||
|
..#.#..#.#..................#..
|
||||||
|
...#...#...#...##......#.......
|
||||||
|
.#...##....#...#...#...#.......
|
||||||
|
.......#.#.......#.............
|
||||||
|
.#.##.#.....#...........#.##.#.
|
||||||
|
.#.##.#........#...##..........
|
||||||
|
.#.....#.....#....#..#.........
|
||||||
|
...##.............##...........
|
||||||
|
.#........##.....#.......#...#.
|
||||||
|
...........#..#..##........##..
|
||||||
|
.....#..#......................
|
||||||
|
..#.......#....................
|
||||||
|
.....#......#....#....#.......#
|
||||||
|
........#..#.#.....#......#....
|
||||||
|
..........#..#.....#......#....
|
||||||
|
..........#####.....#........#.
|
||||||
|
........#..#...#.#....#......#.
|
||||||
|
.........#...#....#.#..........
|
||||||
|
......#....##..........#...#...
|
||||||
|
#..............###.#.#.........
|
||||||
|
.#.#............##......#.#..#.
|
||||||
|
......#........................
|
||||||
|
...#..#......#.......#....#...#
|
||||||
|
.......#....##.....#.#......##.
|
||||||
|
...........#..........#..#.....
|
||||||
|
...........#..#.....###......#.
|
||||||
|
.......#....#..##......#.......
|
||||||
|
.........#.#.#.......#..#...#..
|
||||||
|
.......#.......##.....##...#...
|
||||||
|
..............#....#.....#.....
|
||||||
|
...#....#.....#.#..........##..
|
||||||
|
###.........#.............#....
|
||||||
|
...##......#.#........#....#..#
|
||||||
|
#....###.......#...#.#......##.
|
||||||
|
....#...##.......#......#.....#
|
||||||
|
.....#......#..................
|
||||||
|
#........##....#....#.#........
|
||||||
|
........#.......###...#........
|
||||||
|
........#..#.......###.........
|
||||||
|
..............#......#..#......
|
||||||
|
#......#.....#....#.#..........
|
||||||
|
.#......##.#.#.....#...#.#....#
|
||||||
|
.##...........#..#.##.....#....
|
||||||
|
.....#.....................#...
|
||||||
|
.#..#...#...##.#...#...........
|
||||||
|
.......#.......##..#.#..#......
|
||||||
|
.......##.....#.....#..........
|
||||||
|
.................#.............
|
||||||
|
#........#..#.......##.........
|
||||||
|
#...#..###.#..#....#.#.###.....
|
||||||
|
..#.......#.......#.......#....
|
||||||
|
..............#............##..
|
||||||
|
.#...#..#...##.........#....#..
|
||||||
|
#...........#...#..............
|
||||||
|
.......#.....#......#..#.....#.
|
||||||
|
..........#......#.............
|
||||||
|
##.........###..##.#....#..#.#.
|
||||||
|
..............###..............
|
||||||
|
#..##.............##.....#.....
|
||||||
|
....##...................#..#..
|
||||||
|
....#.....#..............#..#.#
|
||||||
|
........#........##...#.....##.
|
||||||
|
#...........#.##..........##...
|
||||||
|
#......##.....#...............#
|
||||||
|
..##..#....#.................#.
|
||||||
|
#.......##.....................
|
||||||
|
...............#.##..##......#.
|
||||||
|
..#.##..#.#....#.......##......
|
||||||
|
......##....#............##....
|
||||||
|
.#..#..##.....#.##....#........
|
||||||
|
#.........#..........#...#....#
|
||||||
|
...#.......#.............#.#.#.
|
||||||
|
..##............#...##........#
|
||||||
|
.......#.#.#........#..........
|
||||||
|
.....#.............#.....#.....
|
||||||
|
.........#.........#.........#.
|
||||||
|
#.....#....#.......#...........
|
||||||
|
.........#....#.............#.#
|
||||||
|
.##..#.......#...#......#......
|
||||||
|
....#....#....#........#....#..
|
||||||
|
............#.......#..#......#
|
||||||
|
.#............#.##........##...
|
||||||
|
..#...##...#....#...#.#...#..#.
|
||||||
|
#...#..........#..##.........#.
|
||||||
|
..#.........................#.#
|
||||||
|
...........#.........#..#.##...
|
||||||
|
.#..................#..#.......
|
||||||
|
......#......#...........#..#..
|
||||||
|
...##.....#.....#..#.......#...
|
||||||
|
.........#.#.......#......#....
|
||||||
|
...........#................#..
|
||||||
|
.....#...#..#............##....
|
||||||
|
.#.......#..#....#..........#..
|
||||||
|
#.....#..#.....#..##.......##..
|
||||||
|
...#.......#...#....#...#.#..##
|
||||||
|
...#...##......#....#....#.....
|
||||||
|
.......###.#..#.......#......#.
|
||||||
|
........#.#...#..#..#...#....#.
|
||||||
|
....#.........##.#.....#.......
|
||||||
|
....#.........#..##........#...
|
||||||
|
..#...........#......#....#.##.
|
||||||
|
.....................#.........
|
||||||
|
...................##......#..#
|
||||||
|
......#.#.....##..##..........#
|
||||||
|
..#.##........#.#.#..........#.
|
||||||
|
.#.......#...##.#....#....#....
|
||||||
|
#.#......#..#..#.......#.......
|
||||||
|
.............#........#.......#
|
||||||
|
....#...#.....#........#...#...
|
||||||
|
..#..............##..#.........
|
||||||
|
..#.................#..#...##..
|
||||||
|
....#..#...#...................
|
||||||
|
......#.........##.#..#..#...##
|
||||||
|
........#..#....#.......#.#.##.
|
||||||
|
.#...#...........#..........#..
|
||||||
|
##.....#...#............##...#.
|
||||||
|
.##.....#...#..................
|
||||||
|
.#.......####.#..##.##.#......#
|
||||||
|
.............#...#..#..#.......
|
||||||
|
...#.##.........#.#....#.......
|
||||||
|
...........##...##....#....##..
|
||||||
|
........#......#...#...........
|
||||||
|
...........#..#...#....#.##....
|
||||||
|
..##....#..........#....#...#..
|
||||||
|
#....#.#.#.......#.#...........
|
||||||
|
......#............##..........
|
||||||
|
#.#.###..#....#.......#...#....
|
||||||
|
.#......##..#..#.#.........#..#
|
||||||
|
..#.........#........#....#....
|
||||||
|
......##.#.......##....#..#..##
|
||||||
|
.............#...#............#
|
||||||
|
......#......#...#.#.#.##.#....
|
||||||
|
#.#...#.##.....#..............#
|
||||||
|
..........#.............##.##..
|
||||||
|
#......#....#...#.#.#.#..#....#
|
||||||
|
........#........#...#.#......#
|
||||||
|
.....#...........#.............
|
||||||
|
...........#....#..........#...
|
||||||
|
....####...#..##....#.#........
|
||||||
|
.#......#...#..#...........#...
|
||||||
|
#......###..#.##.###...........
|
||||||
|
..#...........#.........#....#.
|
||||||
|
................#.#....#..#.##.
|
||||||
|
...................#......#....
|
||||||
|
....#.#.....#.......#...###.##.
|
||||||
|
.#........#.#....#...#..#...#..
|
||||||
|
....#..###.................#..#
|
||||||
|
.....#.#..#........#......#..#.
|
||||||
|
....#.....#...............#...#
|
||||||
|
............##.#.........#..#..
|
||||||
|
.......#..#..##.#.#...##.......
|
||||||
|
..#..........##..#..#........#.
|
||||||
|
..............#..#...#.........
|
||||||
|
......#.#....#........##.......
|
||||||
|
.#.....##....#..#...#.......##.
|
||||||
|
..............#.##.............
|
||||||
|
#..#..#...##....##.#.....##.#..
|
||||||
|
..#...###..#.........##........
|
||||||
|
........##......#.....#..###...
|
||||||
|
.....#......##.###.............
|
||||||
|
....#.....#.#..#.#..#..........
|
||||||
|
....#..#.......#...........#...
|
||||||
|
.#.............#..#......##....
|
||||||
|
..#.#......#.#.................
|
||||||
|
.......#.#.#............#..#...
|
||||||
|
......###....##............#..#
|
||||||
|
.........#....#......#.........
|
||||||
|
..........#...............#..#.
|
||||||
+59
-2
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/alexchao26/advent-of-code-go/util"
|
"github.com/alexchao26/advent-of-code-go/util"
|
||||||
)
|
)
|
||||||
@@ -15,17 +16,73 @@ func main() {
|
|||||||
|
|
||||||
if part == 1 {
|
if part == 1 {
|
||||||
ans := part1(util.ReadFile("./input.txt"))
|
ans := part1(util.ReadFile("./input.txt"))
|
||||||
|
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
||||||
fmt.Println("Output:", ans)
|
fmt.Println("Output:", ans)
|
||||||
} else {
|
} else {
|
||||||
ans := part2(util.ReadFile("./input.txt"))
|
ans := part2(util.ReadFile("./input.txt"))
|
||||||
|
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
||||||
fmt.Println("Output:", ans)
|
fmt.Println("Output:", ans)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func part1(input string) int {
|
func part1(input string) int {
|
||||||
return 0
|
grid := parseInput(input)
|
||||||
|
return rideSlopes(grid, 3, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func part2(input string) int {
|
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
@@ -1,19 +1,33 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/alexchao26/advent-of-code-go/util"
|
||||||
|
)
|
||||||
|
|
||||||
var tests1 = []struct {
|
var tests1 = []struct {
|
||||||
name string
|
name string
|
||||||
want int
|
want int
|
||||||
input string
|
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) {
|
func TestPart1(t *testing.T) {
|
||||||
for _, test := range tests1 {
|
for _, test := range tests1 {
|
||||||
t.Run(test.name, func(*testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
got := part1(test.input)
|
got := part1(test.input)
|
||||||
if got != test.want {
|
if got != test.want {
|
||||||
t.Errorf("got %v, want %v", got, test.want)
|
t.Errorf("got %v, want %v", got, test.want)
|
||||||
@@ -26,14 +40,24 @@ var tests2 = []struct {
|
|||||||
name string
|
name string
|
||||||
want int
|
want int
|
||||||
input string
|
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) {
|
func TestPart2(t *testing.T) {
|
||||||
for _, test := range tests2 {
|
for _, test := range tests2 {
|
||||||
t.Run(test.name, func(*testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
got := part2(test.input)
|
got := part2(test.input)
|
||||||
if got != test.want {
|
if got != test.want {
|
||||||
t.Errorf("got %v, want %v", got, test.want)
|
t.Errorf("got %v, want %v", got, test.want)
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CopyToClipboard is for macOS
|
||||||
|
func CopyToClipboard(text string) error {
|
||||||
|
command := exec.Command("pbcopy")
|
||||||
|
command.Stdin = bytes.NewReader([]byte(text))
|
||||||
|
|
||||||
|
if err := command.Start(); err != nil {
|
||||||
|
return fmt.Errorf("error starting pbcopy command: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := command.Wait()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error running pbcopy %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
// not a real test... used o run copy to clipboard without a main package
|
||||||
|
func TestCopyToClipboard(t *testing.T) {
|
||||||
|
err := CopyToClipboard("asdfqwert")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error while running CopyToClipboard: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user