-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
202 lines (169 loc) · 4.51 KB
/
main.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
package main
import (
"fmt"
"strings"
aoc "github.com/shraddhaag/aoc/library"
)
func main() {
input := aoc.ReadFileLineByLine("input.txt")
fmt.Println("answer for part 1: ", findWeight(getNorthTitledGrid(input)))
fmt.Println("answer for part 2: ", findWeight(getGridAfterXCycles(input, 1000000000)))
}
var cache map[string]string
func getNorthTitledGrid(input []string) []string {
grid := aoc.Get2DGrid(input)
for i := 0; i < len(input[0]); i++ {
index := -1
for j := 0; j < len(input); j++ {
if grid[j][i] == "." && index == -1 {
index = j
}
if grid[j][i] == "#" {
index = -1
}
if index != -1 && grid[j][i] == "O" {
grid[index][i] = "O"
grid[j][i] = "."
index++
}
}
}
for i, line := range grid {
input[i] = strings.Join(line, "")
}
return input
}
// Basic Idea:
// - Repeated cycling (ie after all N, W, S and E rotation) of the grid
// is bound to give a repeated pattern again. The iteration at which we
// encounter the first repeated pattern is the start of the loop.
// - We also need to count the length of the loop. This the number of iterations
// it takes to arrive again at the same pattern.
// - Now that we have both: the iteration of start of the loop and the loop length,
// we can now figure out what will be the end state at the end of the total number of
// iterations.
//
// we use a cache to save the entries after each 4 direction spin to reduce calculations.
//
// Note: an assumption made here is that the beginning of the loop is encoutered in a reasonable
// number of iterations. If this is not the case, ie if beginning of the loop is encountered after
// a significant number of iterations, this code will be too slow to arrive at a solution in a reasonable time.
func getGridAfterXCycles(input []string, times int) []string {
cache = make(map[string]string)
grid := aoc.Get2DGrid(input)
cacheEntry := computeCacheEntry(grid)
loopStartsAt := -1
loopEntries := []string{}
for i := 0; i < times; i++ {
// check for cache hit, ie if current grid has already been evaluated.
// if yes:
// - if this is the first cache hit: current iteration is start of the loop
// - for each cache hit after the first one, we keep storing the results in a loop slice
// - if the same cache hit is encountered again, we just completed a full loop.
if value, ok := cache[cacheEntry]; ok {
if len(loopEntries) != 0 && value == loopEntries[0] {
break
}
if loopStartsAt == -1 {
loopStartsAt = i
}
loopEntries = append(loopEntries, value)
cacheEntry = value
continue
}
grid = getGridAfterCycle(grid, input)
cache[cacheEntry] = computeCacheEntry(grid)
cacheEntry = cache[cacheEntry]
}
occurance := (times - 1 - loopStartsAt) % len(loopEntries)
cacheEntry = loopEntries[occurance]
return aoc.SplitStringAfter(cacheEntry, len(input[0]))
}
func getGridAfterCycle(grid [][]string, input []string) [][]string {
// north tilt
for i := 0; i < len(input[0]); i++ {
index := -1
for j := 0; j < len(input); j++ {
if grid[j][i] == "." && index == -1 {
index = j
}
if grid[j][i] == "#" {
index = -1
}
if index != -1 && grid[j][i] == "O" {
grid[index][i] = "O"
grid[j][i] = "."
index++
}
}
}
// west tilt
for i := 0; i < len(input); i++ {
index := -1
for j := 0; j < len(input[0]); j++ {
if grid[i][j] == "." && index == -1 {
index = j
}
if grid[i][j] == "#" {
index = -1
}
if index != -1 && grid[i][j] == "O" {
grid[i][index] = "O"
grid[i][j] = "."
index++
}
}
}
// south tilt
for i := len(input[0]) - 1; i >= 0; i-- {
index := -1
for j := len(input) - 1; j >= 0; j-- {
if grid[j][i] == "." && index == -1 {
index = j
}
if grid[j][i] == "#" {
index = -1
}
if index != -1 && grid[j][i] == "O" {
grid[index][i] = "O"
grid[j][i] = "."
index--
}
}
}
// east tilt
for i := len(input) - 1; i >= 0; i-- {
index := -1
for j := len(input[0]) - 1; j >= 0; j-- {
if grid[i][j] == "." && index == -1 {
index = j
}
if grid[i][j] == "#" {
index = -1
}
if index != -1 && grid[i][j] == "O" {
grid[i][index] = "O"
grid[i][j] = "."
index--
}
}
}
return grid
}
func findWeight(input []string) int {
sum := 0
for i, line := range input {
for _, char := range line {
if char == 'O' {
sum += (len(input) - i)
}
}
}
return sum
}
func computeCacheEntry(grid [][]string) (answer string) {
for _, line := range grid {
answer += strings.Join(line, "")
}
return
}