2016-day02: moving within the bounds of some 2D shape

This commit is contained in:
alexchao26
2020-12-20 22:57:33 -05:00
parent 8cfbc9d84e
commit 3412d43ec2
2 changed files with 110 additions and 0 deletions
+84
View File
@@ -0,0 +1,84 @@
package main
import (
"flag"
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/util"
)
func main() {
var part int
flag.IntVar(&part, "part", 1, "part 1 or 2")
flag.Parse()
fmt.Println("Running part", part)
ans := part1(util.ReadFile("./input.txt"), part)
fmt.Println("Output:", ans)
}
func part1(input string, part int) string {
directions := parseInput(input)
var pressedButtons string
// set starting row, col and phone pad based on part number
// pad the borders of phonePad with spaces to make border collision logic
// the same in both parts
// used a space instead of empty string so it's easier to look at...
row, col := 2, 2
phonePad := [][]string{
{" ", " ", " ", " ", " "},
{" ", "1", "2", "3", " "},
{" ", "4", "5", "6", " "},
{" ", "7", "8", "9", " "},
{" ", " ", " ", " ", " "},
}
if part == 2 {
phonePad = [][]string{
{" ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", "1", " ", " ", " "},
{" ", " ", "2", "3", "4", " ", " "},
{" ", "5", "6", "7", "8", "9", " "},
{" ", " ", "A", "B", "C", " ", " "},
{" ", " ", " ", "D", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " "},
}
row, col = 3, 1
}
for _, list := range directions {
for _, direction := range list {
switch direction {
case "U":
if phonePad[row-1][col] != " " {
row--
}
case "D":
if phonePad[row+1][col] != " " {
row++
}
case "L":
if phonePad[row][col-1] != " " {
col--
}
case "R":
if phonePad[row][col+1] != " " {
col++
}
default:
panic("unhandled direction: " + direction)
}
}
pressedButtons += phonePad[row][col]
}
return pressedButtons
}
func parseInput(input string) (ans [][]string) {
for _, line := range strings.Split(input, "\n") {
ans = append(ans, strings.Split(line, ""))
}
return ans
}
+26
View File
@@ -0,0 +1,26 @@
package main
import (
"testing"
"github.com/alexchao26/advent-of-code-go/util"
)
func Test_part1(t *testing.T) {
tests := []struct {
name string
input string
part int
want string
}{
{"actual", util.ReadFile("input.txt"), 1, "12578"},
{"actual", util.ReadFile("input.txt"), 2, "516DD"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := part1(tt.input, tt.part); got != tt.want {
t.Errorf("part1() = %v, want %v", got, tt.want)
}
})
}
}