generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.go
180 lines (161 loc) · 3.43 KB
/
solution.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 day14p2
import (
"encoding/binary"
"fmt"
"hash/fnv"
"io"
"sort"
"aoc/utils"
)
var maxiter = 1000000000
func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
ymax := len(lines)
xmax := len(lines[0])
pos := make(map[utils.Point]rune)
for j, ln := range lines {
for i, r := range ln {
if r != '.' {
p := utils.Point{X: i, Y: ymax - j - 1}
pos[p] = r
}
}
}
platform := Platform{xmax, ymax, pos}
checksums := make(map[uint64]int)
looking := true
for i := 0; i < maxiter; i++ {
tiltCycle(&platform)
cs := platform.checkSum()
if v, ok := checksums[cs]; ok && looking {
interval := i - v
start := v
span := maxiter - start
ncycles := span / interval
i = start + ncycles*interval
looking = false
}
checksums[cs] = i
}
if utils.Verbose {
fmt.Println(platform.checkSum())
platform.print()
}
// measure load
sum := 0
for k, v := range platform.Positions {
if v == 'O' {
sum += k.Y + 1
}
}
return sum
}
type Platform struct {
XMax int
YMax int
Positions map[utils.Point]rune
}
func tiltCycle(platform *Platform) {
// Tilt north
for i := 0; i < platform.XMax; i++ {
blocker := platform.YMax // what blocks the stones
for j := platform.YMax - 1; j >= 0; j-- {
pt := utils.Point{X: i, Y: j}
r := platform.Positions[pt]
switch r {
case 'O':
newPt := utils.Point{X: i, Y: blocker - 1}
delete(platform.Positions, pt)
platform.Positions[newPt] = 'O'
blocker = blocker - 1
case '#':
blocker = j
}
}
}
// Tilt west
for j := 0; j < platform.YMax; j++ {
blocker := -1 // what blocks the stones
for i := 0; i < platform.XMax; i++ {
pt := utils.Point{X: i, Y: j}
r := platform.Positions[pt]
switch r {
case 'O':
newPt := utils.Point{X: blocker + 1, Y: j}
delete(platform.Positions, pt)
platform.Positions[newPt] = 'O'
blocker = blocker + 1
case '#':
blocker = i
}
}
}
// Tilt south
for i := 0; i < platform.XMax; i++ {
blocker := -1 // what blocks the stones
for j := 0; j < platform.YMax; j++ {
pt := utils.Point{X: i, Y: j}
r := platform.Positions[pt]
switch r {
case 'O':
newPt := utils.Point{X: i, Y: blocker + 1}
delete(platform.Positions, pt)
platform.Positions[newPt] = 'O'
blocker = blocker + 1
case '#':
blocker = j
}
}
}
// Tilt east
for j := 0; j < platform.YMax; j++ {
blocker := platform.XMax // what blocks the stones
for i := platform.XMax - 1; i >= 0; i-- {
pt := utils.Point{X: i, Y: j}
r := platform.Positions[pt]
switch r {
case 'O':
newPt := utils.Point{X: blocker - 1, Y: j}
delete(platform.Positions, pt)
platform.Positions[newPt] = 'O'
blocker = blocker - 1
case '#':
blocker = i
}
}
}
}
func (p *Platform) print() {
for j := p.YMax - 1; j >= 0; j-- {
for i := 0; i < p.XMax; i++ {
pt := utils.Point{X: i, Y: j}
r := p.Positions[pt]
if r == 0 {
fmt.Printf(".")
} else {
fmt.Printf("%c", r)
}
}
fmt.Printf("\n")
}
fmt.Printf("\n")
}
func (p *Platform) checkSum() uint64 {
parray := []int{}
for j := p.YMax - 1; j >= 0; j-- {
for i := 0; i < p.XMax; i++ {
pt := utils.Point{X: i, Y: j}
r := p.Positions[pt]
if r == 'O' {
parray = append(parray, p.XMax*j+i)
}
}
}
sort.Ints(parray)
hf := fnv.New64a()
for _, v := range parray {
err := binary.Write(hf, binary.LittleEndian, int32(v))
utils.Check(err, "Error writing binary")
}
return hf.Sum64()
}