/
part2.go
190 lines (170 loc) · 4.5 KB
/
part2.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
package main
import (
"bufio"
"fmt"
"hash/crc32"
"os"
"strconv"
)
const cicles int = 1000000000
var platform [][]byte
func main() {
if len(os.Args) != 2 {
fmt.Println("Please, provide just one file to analize.")
os.Exit(0)
}
fmt.Println("Opening file", os.Args[1])
f, err := os.Open(os.Args[1])
if err != nil {
fmt.Println("Could not open", os.Args[1])
os.Exit(1)
}
defer f.Close()
fmt.Println("File", os.Args[1], "opened")
scn := bufio.NewScanner(f)
// Load input
fmt.Println("Loading ...")
for scn.Scan() {
l := scn.Text()
platform = append(platform, []byte(l))
}
fmt.Println("Platform loaded.")
//printPlatform()
// Tilding
var e bool
var t int
var exitIndex int
mapIndex := make(map[string]int) // Hash --> Index
mapLoad := make(map[int]int) // Index --> Load
l := load()
h := hash()
fmt.Printf("Cicle: %3d, load: %6d, hash: %s\n", 0, l, h)
mapIndex[h] = 0
mapLoad[0] = l
for i := 1; i <= cicles; i++ {
moveRocksNorth()
moveRocksWest()
moveRocksSouth()
moveRocksEast()
l = load()
h = hash()
fmt.Printf("Cicle: %3d, load: %6d, hash: %s\n", i, l, h)
t, e = mapIndex[h]
if e {
fmt.Printf("\n Hash \"%s\" was already found on cicle %d.\n This means we are now into a loop.\n", h, t)
exitIndex = i
break
} else {
mapIndex[h] = i
mapLoad[i] = l
}
}
if e {
fmt.Printf(" We ended at %d because it goes back to %d.\n", exitIndex, t)
fmt.Printf(" The loop is %d - %d = %d units length.\n", exitIndex, t, exitIndex-t)
fmt.Printf(" %d total cicles - %d (starting loop cicle) is %d pending iterations from the start of the loop.\n", cicles, t, cicles-t)
resultIndex := (cicles-t)%(exitIndex-t) + t
fmt.Printf(" %d modus %d is %d + the starting loop cicle (%d) is %d.\n", cicles-t, exitIndex-t, (cicles-t)%(exitIndex-t), t, resultIndex)
fmt.Printf(" The load on the cicle %d will be the same to the cicle %d.\n", cicles, resultIndex)
fmt.Printf(" The load on cicle %d is \033[1m%d\033[0m\n", resultIndex, mapLoad[resultIndex])
} else {
fmt.Println("I can't believe we did", cicles, "iterations.")
}
}
// moveRocksNorth moves all the rocks to the North
func moveRocksNorth() {
for y := 1; y < len(platform); y++ {
for x := 0; x < len(platform[y]); x++ {
if platform[y][x] == 'O' {
displaceRockNorth(x, y)
}
}
}
}
// moveRocksWest moves all the rocks to the West
func moveRocksWest() {
for x := 1; x < len(platform[0]); x++ {
for y := 0; y < len(platform); y++ {
if platform[y][x] == 'O' {
displaceRockWest(x, y)
}
}
}
}
// moveRocksSouth moves all the rocks to the South
func moveRocksSouth() {
for y := len(platform) - 2; y >= 0; y-- {
for x := 0; x < len(platform[y]); x++ {
if platform[y][x] == 'O' {
displaceRockSouth(x, y)
}
}
}
}
// moveRocksEast moves all the rocks to the East
func moveRocksEast() {
for x := len(platform[0]) - 2; x >= 0; x-- {
for y := 0; y < len(platform); y++ {
if platform[y][x] == 'O' {
displaceRockEast(x, y)
}
}
}
}
// displaceRockNorth moves a the rock at (x, y) to the North until it hits an obstacle
func displaceRockNorth(x, y int) {
for y > 0 && platform[y-1][x] == '.' {
platform[y][x], platform[y-1][x] = platform[y-1][x], platform[y][x]
y--
}
}
// displaceRockWest moves a the rock at (x, y) to the West until it hits an obstacle
func displaceRockWest(x, y int) {
for x > 0 && platform[y][x-1] == '.' {
platform[y][x], platform[y][x-1] = platform[y][x-1], platform[y][x]
x--
}
}
// displaceRockSouth moves a the rock at (x, y) to the South until it hits an obstacle
func displaceRockSouth(x, y int) {
for y < len(platform)-1 && platform[y+1][x] == '.' {
platform[y][x], platform[y+1][x] = platform[y+1][x], platform[y][x]
y++
}
}
// displaceRockEast moves a the rock at (x, y) to the East until it hits an obstacle
func displaceRockEast(x, y int) {
for x < len(platform[y])-1 && platform[y][x+1] == '.' {
platform[y][x], platform[y][x+1] = platform[y][x+1], platform[y][x]
x++
}
}
// load returns the sum of each rock times it's row.
func load() int {
var sum int
multiplier := len(platform)
for y := 0; y < len(platform); y++ {
for x := 0; x < len(platform[y]); x++ {
if platform[y][x] == 'O' {
sum += multiplier
}
}
multiplier--
}
return sum
}
// hash returns a string that identifies a unique platform
func hash() string {
h := crc32.NewIEEE()
var p []byte
for i := 0; i < len(platform); i++ {
p = append(p, platform[i]...)
}
h.Write(p)
v := h.Sum32()
s := strconv.FormatInt(int64(v), 16)
for len(s) < 8 {
s = "0" + s
}
return s
}