/
14_2.go
180 lines (159 loc) · 3.34 KB
/
14_2.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
package main
import (
"crypto/sha512"
"log"
"github.com/glennhartmann/aoclib/must"
)
func main() {
lines := must.GetFullInputAsBytes()
m := make(map[string]int, 100)
cycle := 0
prev := 0
for ; ; cycle++ {
tiltUp(lines)
tiltLeft(lines)
tiltDown(lines)
tiltRight(lines)
h := hash(lines)
if _, ok := m[h]; ok {
prev = m[h]
break
}
m[h] = cycle
}
log.Printf("found repeated state at cycle %d (previously saw in cycle %d)", cycle, prev)
period := cycle - prev
log.Printf("period = %d", period)
cycles := 1000000000 - cycle - 1
desiredCycle := cycles % period
log.Printf("want pattern in %d cycles", desiredCycle)
for c := 0; c < desiredCycle; c++ {
tiltUp(lines)
tiltLeft(lines)
tiltDown(lines)
tiltRight(lines)
}
log.Printf("new layout:")
for r := range lines {
log.Printf("%s", string(lines[r]))
}
sum := 0
for r := range lines {
sum += countRow(lines[r]) * (len(lines) - r)
}
log.Printf("sum: %d", sum)
}
func tiltUp(lines [][]byte) {
for c := 0; c < len(lines[0]); c++ {
slot := 0
rounds := 0
for r := 0; r < len(lines); r++ {
if lines[r][c] == '#' {
moveUp(lines, c, slot, rounds)
rounds = 0
slot = r + 1
} else if lines[r][c] == 'O' {
rounds++
lines[r][c] = '.'
}
}
moveUp(lines, c, slot, rounds)
}
}
// yes, I really am just copy/pasting. I don't feel like doing better right now.
func tiltLeft(lines [][]byte) {
for r := 0; r < len(lines); r++ {
slot := 0
rounds := 0
for c := 0; c < len(lines[0]); c++ {
if lines[r][c] == '#' {
moveLeft(lines, r, slot, rounds)
rounds = 0
slot = c + 1
} else if lines[r][c] == 'O' {
rounds++
lines[r][c] = '.'
}
}
moveLeft(lines, r, slot, rounds)
}
}
func tiltDown(lines [][]byte) {
for c := 0; c < len(lines[0]); c++ {
slot := len(lines) - 1
rounds := 0
for r := len(lines) - 1; r >= 0; r-- {
if lines[r][c] == '#' {
moveDown(lines, c, slot, rounds)
rounds = 0
slot = r - 1
} else if lines[r][c] == 'O' {
rounds++
lines[r][c] = '.'
}
}
moveDown(lines, c, slot, rounds)
}
}
func tiltRight(lines [][]byte) {
for r := 0; r < len(lines); r++ {
slot := len(lines[0]) - 1
rounds := 0
for c := len(lines[0]) - 1; c >= 0; c-- {
if lines[r][c] == '#' {
moveRight(lines, r, slot, rounds)
rounds = 0
slot = c - 1
} else if lines[r][c] == 'O' {
rounds++
lines[r][c] = '.'
}
}
moveRight(lines, r, slot, rounds)
}
}
func moveUp(lines [][]byte, c, slot, rounds int) {
for i := 0; i < rounds; i++ {
lines[slot+i][c] = 'O'
}
}
func moveLeft(lines [][]byte, r, slot, rounds int) {
for i := 0; i < rounds; i++ {
lines[r][slot+i] = 'O'
}
}
func moveDown(lines [][]byte, c, slot, rounds int) {
for i := 0; i < rounds; i++ {
lines[slot-i][c] = 'O'
}
}
func moveRight(lines [][]byte, r, slot, rounds int) {
for i := 0; i < rounds; i++ {
lines[r][slot-i] = 'O'
}
}
func countRow(line []byte) int {
count := 0
for c := range line {
if line[c] == 'O' {
count++
}
}
return count
}
func hash(lines [][]byte) string {
b := make([]byte, 0, len(lines)*len(lines[0]))
for r := 0; r < len(lines); r++ {
for c := 0; c < len(lines[0]); c++ {
b = append(b, lines[r][c])
}
}
return stringHash(sha512.Sum512(b))
}
func stringHash(b [64]byte) string {
c := make([]byte, 64)
for i := 0; i < 64; i++ {
c[i] = b[i]
}
return string(c)
}