-
Notifications
You must be signed in to change notification settings - Fork 0
/
d15.go
221 lines (209 loc) · 3.64 KB
/
d15.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
218
219
220
221
package main
import (
"fmt"
"strconv"
"strings"
)
type d15warehouse struct {
width int64
height int64
area [][]byte
robot *coords
moves []byte
step int
wide bool
}
func (wh *d15warehouse) getVel() *coords {
if wh.step == len(wh.moves) {
return nil
}
c := coords{}
switch wh.moves[wh.step] {
case '^':
c.y = -1
case '>':
c.x = 1
case 'v':
c.y = 1
case '<':
c.x = -1
}
return &c
}
func (wh *d15warehouse) push(x, y int64, dry bool) bool {
s := wh.area[y][x]
if s == '#' {
return false
}
if s == '.' {
return true
}
v := wh.getVel()
if v == nil {
return false
}
nx := x + v.x
ny := y + v.y
if !wh.wide {
if !wh.push(nx, ny, dry) {
return false
}
if !dry {
wh.area[y][x] = '.'
wh.area[ny][nx] = 'O'
}
return true
}
if v.x != 0 {
nx2 := nx + v.x
ny2 := ny + v.y
if !wh.push(nx2, ny2, dry) {
return false
}
if !dry {
wh.area[ny2][nx2] = wh.area[ny][nx]
wh.area[ny][nx] = wh.area[y][x]
wh.area[y][x] = '.'
}
return true
}
x2 := x + 1
y2 := y
if s == ']' {
x2 -= 2
}
nx2 := x2 + v.x
ny2 := y2 + v.y
if !wh.push(nx, ny, true) || !wh.push(nx2, ny2, true) {
return false
}
wh.push(nx, ny, dry)
wh.push(nx2, ny2, dry)
if !dry {
wh.area[ny2][nx2] = wh.area[y2][x2]
wh.area[ny][nx] = wh.area[y][x]
wh.area[y2][x2] = '.'
wh.area[y][x] = '.'
}
return true
}
func (wh *d15warehouse) move() bool {
v := wh.getVel()
if v == nil {
return false
}
x := wh.robot.x + v.x
y := wh.robot.y + v.y
if wh.push(x, y, false) {
wh.robot.x = x
wh.robot.y = y
}
wh.step++
return true
}
func (wh *d15warehouse) getSum() (sum int) {
for y, row := range wh.area {
for x, s := range row {
if s == 'O' || s == '[' {
sum += y*100 + x
}
}
}
return
}
func (wh *d15warehouse) print() (out string) {
out += fmt.Sprintf("Next move %s, step %d:\n", string(wh.moves[wh.step]), wh.step)
for y, row := range wh.area {
for x, spot := range row {
if wh.robot.x == int64(x) && wh.robot.y == int64(y) {
out += string(wh.moves[wh.step])
continue
}
out += string(spot)
}
out += "\n"
}
return
}
func (*methods) D15P1(input string) string {
parts := strings.Split(input, "\n\n")
lines := strings.Split(parts[0], "\n")
var robot coords
var area [][]byte
for y, l := range lines {
var row []byte
for x, s := range l {
if s == '@' {
robot.x = int64(x)
robot.y = int64(y)
row = append(row, '.')
continue
}
row = append(row, byte(s))
}
area = append(area, row)
}
var moves []byte
for _, m := range parts[1] {
moves = append(moves, byte(m))
}
wh := &d15warehouse{
width: int64(len(area[0])),
height: int64(len(area)),
area: area,
robot: &robot,
moves: moves,
}
for {
if !wh.move() {
break
}
}
return strconv.Itoa(wh.getSum())
}
func (*methods) D15P2(input string) string {
parts := strings.Split(input, "\n\n")
lines := strings.Split(parts[0], "\n")
var robot coords
var area [][]byte
for _, l := range lines {
var row []byte
for _, s := range l {
if s == '@' {
robot.x = int64(len(row))
robot.y = int64(len(area))
row = append(row, '.')
row = append(row, '.')
continue
}
if s == 'O' {
row = append(row, '[')
row = append(row, ']')
continue
}
row = append(row, byte(s))
row = append(row, byte(s))
}
area = append(area, row)
}
var moves []byte
for _, m := range parts[1] {
if m != '\n' {
moves = append(moves, byte(m))
}
}
wh := &d15warehouse{
width: int64(len(area[0])),
height: int64(len(area)),
area: area,
robot: &robot,
moves: moves,
wide: true,
}
for {
if !wh.move() {
break
}
}
return strconv.Itoa(wh.getSum())
}