From 3412d43ec248572f45b62b85cf69c9403517b505 Mon Sep 17 00:00:00 2001 From: alexchao26 Date: Sun, 20 Dec 2020 22:57:33 -0500 Subject: [PATCH] 2016-day02: moving within the bounds of some 2D shape --- 2016/day02/main.go | 84 +++++++++++++++++++++++++++++++++++++++++ 2016/day02/main_test.go | 26 +++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 2016/day02/main.go create mode 100644 2016/day02/main_test.go diff --git a/2016/day02/main.go b/2016/day02/main.go new file mode 100644 index 0000000..00ce0d3 --- /dev/null +++ b/2016/day02/main.go @@ -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 +} diff --git a/2016/day02/main_test.go b/2016/day02/main_test.go new file mode 100644 index 0000000..9aec0ef --- /dev/null +++ b/2016/day02/main_test.go @@ -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) + } + }) + } +}