|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +type P struct{ x, y int } |
| 12 | + |
| 13 | +var numKeypadMap = map[byte]P{ |
| 14 | + '7': {0, 0}, '8': {0, 1}, '9': {0, 2}, |
| 15 | + '4': {1, 0}, '5': {1, 1}, '6': {1, 2}, |
| 16 | + '1': {2, 0}, '2': {2, 1}, '3': {2, 2}, |
| 17 | + '_': {3, 0}, '0': {3, 1}, 'A': {3, 2}, |
| 18 | +} |
| 19 | + |
| 20 | +var dirKeypadMap = map[byte]P{ |
| 21 | + '_': {0, 0}, '^': {0, 1}, 'A': {0, 2}, |
| 22 | + '<': {1, 0}, 'v': {1, 1}, '>': {1, 2}, |
| 23 | +} |
| 24 | + |
| 25 | +func main() { |
| 26 | + codes := parseInput() |
| 27 | + sum := 0 |
| 28 | + for _, code := range codes { |
| 29 | + directions := pressNumbers(code) |
| 30 | + seqLength := press(directions, Depth) |
| 31 | + |
| 32 | + numericPart, _ := strconv.Atoi(code[:len(code)-1]) |
| 33 | + |
| 34 | + sum += seqLength * numericPart |
| 35 | + } |
| 36 | + fmt.Println(sum) |
| 37 | +} |
| 38 | + |
| 39 | +// type for the key in the map for press() memoization |
| 40 | +type key = struct { |
| 41 | + seq string |
| 42 | + depth int |
| 43 | +} |
| 44 | + |
| 45 | +// memoization for press() |
| 46 | +var cachePress = map[key]int{} |
| 47 | + |
| 48 | +// press returns the length of the directional string |
| 49 | +// after applying `depth` times the indirection |
| 50 | +func press(code string, depth int) int { |
| 51 | + if depth == 0 { |
| 52 | + return len(code) |
| 53 | + } |
| 54 | + |
| 55 | + // check in cache |
| 56 | + if size, ok := cachePress[key{code, depth}]; ok { |
| 57 | + return size |
| 58 | + } |
| 59 | + |
| 60 | + totalSize := 0 |
| 61 | + for _, chunk := range strings.SplitAfter(code, "A") { |
| 62 | + totalSize += press(pressDirections(chunk), depth-1) |
| 63 | + } |
| 64 | + |
| 65 | + // save into cache |
| 66 | + cachePress[key{code, depth}] = totalSize |
| 67 | + |
| 68 | + return totalSize |
| 69 | +} |
| 70 | + |
| 71 | +func pressDirections(dir string) string { |
| 72 | + hole := dirKeypadMap['_'] |
| 73 | + curr := dirKeypadMap['A'] |
| 74 | + |
| 75 | + seq := "" |
| 76 | + for _, c := range dir { |
| 77 | + goal := dirKeypadMap[byte(c)] |
| 78 | + |
| 79 | + deltaH, deltaV := goal.y-curr.y, goal.x-curr.x |
| 80 | + hs, vs := getMovements(deltaH, deltaV) |
| 81 | + seq += prioritize(hs, vs, curr, goal, hole) |
| 82 | + |
| 83 | + curr.x += deltaV |
| 84 | + curr.y += deltaH |
| 85 | + |
| 86 | + seq += "A" |
| 87 | + } |
| 88 | + return seq |
| 89 | +} |
| 90 | + |
| 91 | +func pressNumbers(code string) string { |
| 92 | + hole := numKeypadMap['_'] |
| 93 | + curr := numKeypadMap['A'] |
| 94 | + |
| 95 | + seq := "" |
| 96 | + for _, c := range code { |
| 97 | + goal := numKeypadMap[byte(c)] |
| 98 | + |
| 99 | + deltaH, deltaV := goal.y-curr.y, goal.x-curr.x |
| 100 | + hs, vs := getMovements(deltaH, deltaV) |
| 101 | + seq += prioritize(hs, vs, curr, goal, hole) |
| 102 | + |
| 103 | + curr.x += deltaV |
| 104 | + curr.y += deltaH |
| 105 | + |
| 106 | + seq += "A" |
| 107 | + } |
| 108 | + return seq |
| 109 | +} |
| 110 | + |
| 111 | +func getMovements(deltaH, deltaV int) (string, string) { |
| 112 | + var hs, vs string |
| 113 | + for deltaH > 0 { |
| 114 | + hs += ">" |
| 115 | + deltaH-- |
| 116 | + } |
| 117 | + for deltaH < 0 { |
| 118 | + hs += "<" |
| 119 | + deltaH++ |
| 120 | + } |
| 121 | + for deltaV > 0 { |
| 122 | + vs += "v" |
| 123 | + deltaV-- |
| 124 | + } |
| 125 | + for deltaV < 0 { |
| 126 | + vs += "^" |
| 127 | + deltaV++ |
| 128 | + } |
| 129 | + return hs, vs |
| 130 | +} |
| 131 | + |
| 132 | +func prioritize(hs, vs string, from, to, avoid P) string { |
| 133 | + switch { |
| 134 | + case from.x == avoid.x && to.y == avoid.y: |
| 135 | + return vs + hs |
| 136 | + case from.y == avoid.y && to.x == avoid.x: |
| 137 | + return hs + vs |
| 138 | + case strings.Contains(hs, "<"): |
| 139 | + return hs + vs |
| 140 | + default: |
| 141 | + return vs + hs |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func parseInput() []string { |
| 146 | + var list []string |
| 147 | + scanner := bufio.NewScanner(os.Stdin) |
| 148 | + for scanner.Scan() { |
| 149 | + list = append(list, scanner.Text()) |
| 150 | + } |
| 151 | + return list |
| 152 | +} |
0 commit comments