aoc2016/archive/day02/day.go

62 lines
1.1 KiB
Go

package day02
import (
"tools"
)
func getCodeFromDirections(keypad [][]string, startX, startY int, instructions []string) string {
x := startX
y := startY
code := ""
for _, line := range instructions {
for _, inst := range line {
switch inst {
case 'L':
if x > 0 && keypad[y][x-1] != "x" {
x--
}
case 'R':
if x < len(keypad[0])-1 && keypad[y][x+1] != "x" {
x++
}
case 'U':
if y > 0 && keypad[y-1][x] != "x" {
y--
}
case 'D':
if y < len(keypad)-1 && keypad[y+1][x] != "x" {
y++
}
}
}
code += keypad[y][x]
}
return code
}
func Part1(puzzle tools.AoCPuzzle) interface{} {
instructions := puzzle.GetInputArray()
keypad := [][]string{
{"1", "2", "3"},
{"4", "5", "6"},
{"7", "8", "9"},
}
return getCodeFromDirections(keypad, 1, 1, instructions)
}
func Part2(puzzle tools.AoCPuzzle) interface{} {
instructions := puzzle.GetInputArray()
keypad := [][]string{
{"x", "x", "1", "x", "x"},
{"x", "2", "3", "4", "x"},
{"5", "6", "7", "8", "9"},
{"x", "A", "B", "C", "x"},
{"x", "x", "D", "x", "x"},
}
return getCodeFromDirections(keypad, 0, 2, instructions)
}