-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14p1.go
217 lines (195 loc) · 4.99 KB
/
14p1.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
package main
import (
"fmt"
"aoc2023/utils"
)
type rock struct {
position utils.Coordinate
square bool
}
func D14P1() {
lines := utils.ReadLines("inputs/14.txt")
rocks := parseRocks(lines)
rocks = rollRocks(rocks, len(lines[0]), len(lines), 'N')
totalLoad := calculateRockLoad(rocks, len(lines))
fmt.Println("Total load:", totalLoad)
}
func parseRocks(lines []string) map[utils.Coordinate]rock {
rocks := map[utils.Coordinate]rock{}
for y, line := range lines {
for x, char := range line {
if char == '#' {
rocks[utils.Coordinate{X: x, Y: y}] = rock{utils.Coordinate{X: x, Y: y}, true}
}
if char == 'O' {
rocks[utils.Coordinate{X: x, Y: y}] = rock{utils.Coordinate{X: x, Y: y}, false}
}
}
}
return rocks
}
func drawRocks(rocks map[utils.Coordinate]rock, width int, height int) {
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
rock, ok := rocks[utils.Coordinate{X: x, Y: y}]
if ok {
if rock.square {
fmt.Print("#")
} else {
fmt.Print("O")
}
} else {
fmt.Print(".")
}
}
fmt.Println()
}
}
// TODO: Lots of repeated code here, need to come back and refactor
func rollRocks(
rocks map[utils.Coordinate]rock,
width int,
height int,
direction rune,
) map[utils.Coordinate]rock {
if direction == 'N' {
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
rock, ok := rocks[utils.Coordinate{X: x, Y: y}]
if !ok || rock.square {
continue
}
yPosition := y - 1
for yPosition >= 0 {
_, ok := rocks[utils.Coordinate{X: x, Y: yPosition}]
if ok {
newRock := rock
newRock.position = utils.Coordinate{X: x, Y: yPosition + 1}
rocks[utils.Coordinate{X: x, Y: yPosition + 1}] = newRock
if yPosition+1 != y {
delete(rocks, utils.Coordinate{X: x, Y: y})
}
break
}
if yPosition == 0 {
newRock := rock
newRock.position = utils.Coordinate{X: x, Y: yPosition}
rocks[utils.Coordinate{X: x, Y: yPosition}] = newRock
if yPosition != y {
delete(rocks, utils.Coordinate{X: x, Y: y})
}
}
yPosition--
}
}
}
} else if direction == 'S' {
for y := height - 1; y >= 0; y-- {
for x := 0; x < width; x++ {
rock, ok := rocks[utils.Coordinate{X: x, Y: y}]
if !ok || rock.square {
continue
}
yPosition := y + 1
for yPosition < height {
_, ok := rocks[utils.Coordinate{X: x, Y: yPosition}]
if ok {
newRock := rock
newRock.position = utils.Coordinate{X: x, Y: yPosition - 1}
rocks[utils.Coordinate{X: x, Y: yPosition - 1}] = newRock
if yPosition-1 != y {
delete(rocks, utils.Coordinate{X: x, Y: y})
}
break
}
if yPosition == height-1 {
newRock := rock
newRock.position = utils.Coordinate{X: x, Y: yPosition}
rocks[utils.Coordinate{X: x, Y: yPosition}] = newRock
if yPosition != y {
delete(rocks, utils.Coordinate{X: x, Y: y})
}
}
yPosition++
}
}
}
} else if direction == 'E' {
for x := width - 1; x >= 0; x-- {
for y := 0; y < height; y++ {
rock, ok := rocks[utils.Coordinate{X: x, Y: y}]
if !ok || rock.square {
continue
}
xPosition := x + 1
for xPosition < width {
_, ok := rocks[utils.Coordinate{X: xPosition, Y: y}]
if ok {
newRock := rock
newRock.position = utils.Coordinate{X: xPosition - 1, Y: y}
rocks[utils.Coordinate{X: xPosition - 1, Y: y}] = newRock
if xPosition-1 != x {
delete(rocks, utils.Coordinate{X: x, Y: y})
}
break
}
if xPosition == width-1 {
newRock := rock
newRock.position = utils.Coordinate{X: xPosition, Y: y}
rocks[utils.Coordinate{X: xPosition, Y: y}] = newRock
if xPosition != x {
delete(rocks, utils.Coordinate{X: x, Y: y})
}
}
xPosition++
}
}
}
} else if direction == 'W' {
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
rock, ok := rocks[utils.Coordinate{X: x, Y: y}]
if !ok || rock.square {
continue
}
xPosition := x - 1
for xPosition >= 0 {
_, ok := rocks[utils.Coordinate{X: xPosition, Y: y}]
if ok {
newRock := rock
newRock.position = utils.Coordinate{X: xPosition + 1, Y: y}
rocks[utils.Coordinate{X: xPosition + 1, Y: y}] = newRock
if xPosition+1 != x {
delete(rocks, utils.Coordinate{X: x, Y: y})
}
break
}
if xPosition == 0 {
newRock := rock
newRock.position = utils.Coordinate{X: xPosition, Y: y}
rocks[utils.Coordinate{X: xPosition, Y: y}] = newRock
if xPosition != x {
delete(rocks, utils.Coordinate{X: x, Y: y})
}
}
xPosition--
}
}
}
}
return rocks
}
func calculateRockLoad(rocks map[utils.Coordinate]rock, rowMultiplier int) int {
totalLoad := 0
currentRow := 0
for rowMultiplier > 0 {
for _, rock := range rocks {
if rock.position.Y == currentRow && !rock.square {
totalLoad += rowMultiplier
}
}
rowMultiplier--
currentRow++
}
return totalLoad
}