-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd02.go
203 lines (188 loc) · 3.86 KB
/
d02.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"bufio"
"bytes"
"container/list"
"log"
"os"
"strconv"
)
var (
keypad [][]int
bigKeypad [][]string
xPosition = 1
yPosition = 1
)
func decX() {
if xPosition > 0 {
xPosition = xPosition - 1
}
}
func incX() {
if xPosition < 2 {
xPosition = xPosition + 1
}
}
func decY() {
if yPosition > 0 {
yPosition = yPosition - 1
}
}
func incY() {
if yPosition < 2 {
yPosition = yPosition + 1
}
}
/* Here comes the special logic for the extended bigKeypad
1
2 3 4
5 6 7 8 9
A B C
D
*/
func minPosition(pos int) int {
var retc = 0
switch pos {
case 0, 4:
retc = 2
case 1, 3:
retc = 1
case 2:
retc = 0
}
return retc
}
func maxPosition(pos int) int {
var retc = 0
switch pos {
case 0, 4:
retc = 2
case 1, 3:
retc = 3
case 2:
retc = 4
}
return retc
}
func decBigX() {
minXPosition := minPosition(yPosition)
if xPosition > minXPosition {
xPosition = xPosition - 1
}
}
func incBigX() {
maxXPosition := maxPosition(yPosition)
if xPosition < maxXPosition {
xPosition = xPosition + 1
}
}
func decBigY() {
minYPos := minPosition(xPosition)
if yPosition > minYPos {
yPosition = yPosition - 1
}
}
func incBigY() {
maxYPoisition := maxPosition(xPosition)
if yPosition < maxYPoisition {
yPosition = yPosition + 1
}
}
func processCharacter(character string) {
switch character {
case "U":
decX()
case "D":
incX()
case "L":
decY()
case "R":
incY()
default:
log.Fatalf("something went wrong with character %v /n", character)
}
}
func processCharacterBig(character string) {
switch character {
case "U":
decBigX()
case "D":
incBigX()
case "L":
decBigY()
case "R":
incBigY()
default:
log.Fatalf("something went wrong with character %v /n", character)
}
}
func processLine(line string) int {
for index := 0; index < len(line); index++ {
character := string(line[index])
processCharacter(character)
}
return keypad[xPosition][yPosition]
}
func processLineBig(line string) string {
for index := 0; index < len(line); index++ {
character := string(line[index])
processCharacterBig(character)
}
return bigKeypad[xPosition][yPosition]
}
func d2() string {
log.Printf("Day 2\n")
// Init KeyPad
keypad = append(keypad, []int{1, 2, 3})
keypad = append(keypad, []int{4, 5, 6})
keypad = append(keypad, []int{7, 8, 9})
file, err := os.Open("input/d2.txt")
if err != nil {
log.Fatalf("Failed to read input file %v \n", err)
}
defer file.Close()
reader := bufio.NewReader(file)
scanner := bufio.NewScanner(reader)
resultList := list.New()
for scanner.Scan() {
textLine := scanner.Text()
result := processLine(textLine)
resultList.PushBack(result)
}
var bu bytes.Buffer
for element := resultList.Front(); element != nil; element = element.Next() {
bu.WriteString(strconv.Itoa(element.Value.(int)))
}
log.Printf("%v is the result of Part 1\n", bu.String())
return bu.String()
}
func d2Part2() string {
log.Printf("Day 2 Part 2 \n")
bigKeypad = append(bigKeypad, []string{"X", "X", "1", "X", "X"})
bigKeypad = append(bigKeypad, []string{"X", "2", "3", "4", "X"})
bigKeypad = append(bigKeypad, []string{"5", "6", "7", "8", "9"})
bigKeypad = append(bigKeypad, []string{"X", "A", "B", "C", "X"})
bigKeypad = append(bigKeypad, []string{"X", "X", "D", "X", "X"})
// Starting at virtual position 5
xPosition = 0
yPosition = 2
file, err := os.Open("input/d2.txt")
if err != nil {
log.Fatalf("Failed to read input file %v \n", err)
}
defer file.Close()
reader := bufio.NewReader(file)
scanner := bufio.NewScanner(reader)
resultList := list.New()
for scanner.Scan() {
textLine := scanner.Text()
result := processLineBig(textLine)
resultList.PushBack(result)
}
var bu bytes.Buffer
for element := resultList.Front(); element != nil; element = element.Next() {
bu.WriteString(element.Value.(string))
}
log.Printf("%v is the result of Part 1\n", bu.String())
return bu.String()
}