-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDayTwentyTwo.go
273 lines (212 loc) · 5.42 KB
/
DayTwentyTwo.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package adventofcode2017
import (
"fmt"
"strconv"
"strings"
)
const NORTH = 1
const SOUTH = 2
const EAST = 3
const WEST = 4
const CLEAN = 0
const INFECTED = 1
const CLEANED = 2
const WEAKENED = 3
const FLAGGED = 4
func turnRight(currentDir int) int {
switch(currentDir) {
case NORTH:
return EAST
case EAST:
return SOUTH
case SOUTH:
return WEST
case WEST:
return NORTH
default:
return NORTH
}
}
func turnLeft(currentDir int) int {
switch(currentDir) {
case NORTH:
return WEST
case WEST:
return SOUTH
case SOUTH:
return EAST
case EAST:
return NORTH
default:
return NORTH
}
}
func moveVirusCarrier(x,y,currentDirection int) (newX,newY int) {
switch(currentDirection) {
case NORTH:
newX = x
newY = y+1
break
case EAST:
newX = x+1
newY = y
break
case SOUTH:
newX = x
newY = y-1
break
case WEST:
newX = x-1
newY = y
break
default:
newX = x
newY = y
}
return newX,newY
}
var infectionCount int = 0
func virusCarrierDoWork(x,y,currentDirection int,grid map[string]int) (int,int, int) {
//if current node is infect - turn right else turn left
currentPosString := xyToString(x,y)
currentState := grid[currentPosString]
if currentState == INFECTED {
currentDirection = turnRight(currentDirection)
} else {
currentDirection = turnLeft(currentDirection)
}
//if current node is clean - node becomes infected else becomes cleaned
if currentState != INFECTED {
grid[currentPosString] = INFECTED
infectionCount++
} else {
grid[currentPosString] = CLEANED
}
//move one node forward
x,y = moveVirusCarrier(x,y,currentDirection)
return x,y,currentDirection
}
func evolvedVirusCarrierDoWork(x,y,currentDirection int,grid map[string]int) (int,int, int) {
currentPosString := xyToString(x,y)
currentState := grid[currentPosString]
//if node is clean turn left
if currentState == INFECTED {
currentDirection = turnRight(currentDirection)
grid[currentPosString] = FLAGGED
}
//if node is weaked do not turn
if currentState == WEAKENED {
grid[currentPosString] = INFECTED
infectionCount++
}
//if infected turn right
if currentState == CLEAN {
currentDirection = turnLeft(currentDirection)
grid[currentPosString] = WEAKENED
}
//if flagged reverse direction
if currentState == FLAGGED {
currentDirection = turnLeft(turnLeft(currentDirection))
grid[currentPosString] = CLEAN
}
//move one node forward
x,y = moveVirusCarrier(x,y,currentDirection)
return x,y,currentDirection
}
func xyToString(x,y int) string {
return strconv.Itoa(x) + "," + strconv.Itoa(y)
}
func DayTwentyTwoExample() {
fmt.Println("Day 22 - Example")
x,y := 0,0
dir := NORTH
fmt.Println("x:",x,"y:",y)
fmt.Println("Current Direction:",dir)
grid := make(map[string]int)
inputGrid := "..#\n#..\n..."
gridRows := strings.Split(inputGrid, "\n")
gridSize := len(gridRows)
fmt.Println("Grid Size:", gridSize)
centerX := gridSize /2
centerY := centerX
fmt.Println("center point, x:", centerX, "y:", centerY)
for gridY := 0 ; gridY < len(gridRows) ; gridY++ {
fmt.Println("Grid Row:", gridY)
fmt.Println("Gird Row - center:", (gridY -centerY)*-1)
convertedY := (gridY-centerY)*-1
for gridX := 0 ; gridX < len(gridRows[gridY]); gridX++ {
convertedX := (gridX - centerX)
if string(gridRows[gridY][gridX]) == "#" {
grid[xyToString(convertedX,convertedY)] = INFECTED
}
}
}
fmt.Println(grid)
for i:= 0 ; i< 10000; i++ {
x,y,dir = virusCarrierDoWork(x,y,dir,grid)
}
fmt.Println("x:",x,"y:",y)
fmt.Println("Current Direction:",dir)
fmt.Println("Infection Count:", infectionCount)
}
func DayTwentyTwoPartOne() {
fmt.Println("Day 22 - Part One")
x,y := 0,0
dir := NORTH
fmt.Println("x:",x,"y:",y)
fmt.Println("Current Direction:",dir)
grid := make(map[string]int)
inputGrid := ReadFile("day22-input.txt")
gridRows := strings.Split(inputGrid, "\n")
gridSize := len(gridRows)
fmt.Println("Grid Size:", gridSize)
centerX := gridSize /2
centerY := centerX
fmt.Println("center point, x:", centerX, "y:", centerY)
initialInfectionCount := 0
for gridY := 0 ; gridY < len(gridRows) ; gridY++ {
convertedY := (gridY-centerY)*-1
for gridX := 0 ; gridX < len(gridRows[gridY]); gridX++ {
convertedX := (gridX - centerX)
if string(gridRows[gridY][gridX]) == "#" {
grid[xyToString(convertedX,convertedY)] = INFECTED
initialInfectionCount++
}
}
}
infectionCount = 0
for i:= 0 ; i< 10000; i++ {
x,y,dir = virusCarrierDoWork(x,y,dir,grid)
}
fmt.Println("x:",x,"y:",y)
fmt.Println("Current Direction:",dir)
fmt.Println("Infection Count:", infectionCount)
fmt.Println("Initial Infection Count:", initialInfectionCount)
}
func DayTwentyTwoPartTwo() {
fmt.Println("Day 22 - Part Two")
x,y := 0,0
dir := NORTH
grid := make(map[string]int)
inputGrid := ReadFile("day22-input.txt")
gridRows := strings.Split(inputGrid, "\n")
gridSize := len(gridRows)
centerX := gridSize /2
centerY := centerX
for gridY := 0 ; gridY < len(gridRows) ; gridY++ {
convertedY := (gridY-centerY)*-1
for gridX := 0 ; gridX < len(gridRows[gridY]); gridX++ {
convertedX := (gridX - centerX)
if string(gridRows[gridY][gridX]) == "#" {
grid[xyToString(convertedX,convertedY)] = INFECTED
}
}
}
infectionCount = 0
for i:= 0 ; i< 10000000; i++ {
x,y,dir = evolvedVirusCarrierDoWork(x,y,dir,grid)
}
fmt.Println("x:",x,"y:",y)
fmt.Println("Current Direction:",dir)
fmt.Println("Infection Count:", infectionCount)
}