-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15.rb
235 lines (204 loc) · 5.76 KB
/
15.rb
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# https://adventofcode.com/2024/day/15
lines = File.readlines('input15.txt').map(&:chomp)
separator_index = lines.index('')
room = lines[..separator_index - 1].map { |l| l.chars }
path = lines[separator_index + 1..].join
########################################################################################################################
# 1
########################################################################################################################
room_dup = room.map(&:dup)
start_y = room_dup.index { |r| r.include?('@') }
start_x = room_dup[start_y].index('@')
path.each_char do |dir|
y, x = start_y, start_x
if dir == '^'
loop do
y -= 1
break if room_dup[y][x] == '#'
if room_dup[y][x] == '.'
y.upto(start_y - 1).each do |i|
room_dup[i][x] = room_dup[i + 1][x]
end
room_dup[start_y][start_x] = '.'
start_y -= 1
break
end
end
elsif dir == 'v'
loop do
y += 1
break if room_dup[y][x] == '#'
if room_dup[y][x] == '.'
y.downto(start_y + 1).each do |i|
room_dup[i][x] = room_dup[i - 1][x]
end
room_dup[start_y][start_x] = '.'
start_y += 1
break
end
end
elsif dir == '>'
loop do
x += 1
break if room_dup[y][x] == '#'
if room_dup[y][x] == '.'
x.downto(start_x + 1).each do |i|
room_dup[y][i] = room_dup[y][i - 1]
end
room_dup[start_y][start_x] = '.'
start_x += 1
break
end
end
elsif dir == '<'
loop do
x -= 1
break if room_dup[y][x] == '#'
if room_dup[y][x] == '.'
x.upto(start_x - 1).each do |i|
room_dup[y][i] = room_dup[y][i + 1]
end
room_dup[start_y][start_x] = '.'
start_x -= 1
break
end
end
end
end
result = 0
room_dup.each_index do |y_coord|
room_dup[y_coord].each_index do |x_coord|
result += x_coord + y_coord * 100 if room_dup[y_coord][x_coord] == 'O'
end
end
puts result
# 1559280
########################################################################################################################
# 2
########################################################################################################################
@updated_room = []
room.each do |row|
updated_row = []
row.each do |c|
if c == '@'
updated_row += ['@', '.']
elsif c == 'O'
updated_row += ['[', ']']
else
updated_row += [c, c]
end
end
@updated_room << updated_row
end
start_y = @updated_room.index { |r| r.include?('@') }
start_x = @updated_room[start_y].index('@')
def boxes_up(box_coords, y, x)
if @updated_room[y - 1][x] == '['
box_coords << [y - 1, x]
boxes_up(box_coords, y - 1, x)
boxes_up(box_coords, y - 1, x + 1)
elsif @updated_room[y - 1][x] == ']'
box_coords << [y - 1, x - 1]
boxes_up(box_coords, y - 1, x - 1)
boxes_up(box_coords, y - 1, x)
end
end
def boxes_down(box_coords, y, x)
if @updated_room[y + 1][x] == '['
box_coords << [y + 1, x]
boxes_down(box_coords, y + 1, x)
boxes_down(box_coords, y + 1, x + 1)
elsif @updated_room[y + 1][x] == ']'
box_coords << [y + 1, x - 1]
boxes_down(box_coords, y + 1, x - 1)
boxes_down(box_coords, y + 1, x)
end
end
path.each_char do |dir|
y, x = start_y, start_x
if dir == '>'
loop do
x += 1
break if @updated_room[y][x] == '#'
if @updated_room[y][x] == '.'
x.downto(start_x + 1).each do |i|
@updated_room[y][i] = @updated_room[y][i - 1]
end
@updated_room[start_y][start_x] = '.'
start_x += 1
break
end
end
elsif dir == '<'
loop do
x -= 1
break if @updated_room[y][x] == '#'
if @updated_room[y][x] == '.'
x.upto(start_x - 1).each do |i|
@updated_room[y][i] = @updated_room[y][i + 1]
end
@updated_room[start_y][start_x] = '.'
start_x -= 1
break
end
end
elsif dir == '^'
move = false
if @updated_room[y - 1][x] == '[' || @updated_room[y - 1][x] == ']'
box_coords = []
boxes_up(box_coords, y, x)
can_move = box_coords.all? do |box_y, box_x|
@updated_room[box_y - 1][box_x] != '#' && @updated_room[box_y - 1][box_x + 1] != '#'
end
if can_move
box_coords.sort_by(&:first).each do |box_y, box_x|
@updated_room[box_y - 1][box_x] = '['
@updated_room[box_y - 1][box_x + 1] = ']'
@updated_room[box_y][box_x] = '.'
@updated_room[box_y][box_x + 1] = '.'
end
move = true
end
else
move = true if @updated_room[y - 1][x] == '.'
end
if move
@updated_room[y - 1][x] = '@'
@updated_room[y][x] = '.'
start_y -= 1
end
elsif dir == 'v'
move = false
if @updated_room[y + 1][x] == '[' || @updated_room[y + 1][x] == ']'
box_coords = []
boxes_down(box_coords, y, x)
can_move = box_coords.all? do |box_y, box_x|
@updated_room[box_y + 1][box_x] != '#' && @updated_room[box_y + 1][box_x + 1] != '#'
end
if can_move
box_coords.sort_by(&:first).reverse.each do |box_y, box_x|
@updated_room[box_y + 1][box_x] = '['
@updated_room[box_y + 1][box_x + 1] = ']'
@updated_room[box_y][box_x] = '.'
@updated_room[box_y][box_x + 1] = '.'
end
move = true
end
else
move = true if @updated_room[y + 1][x] == '.'
end
if move
@updated_room[y + 1][x] = '@'
@updated_room[y][x] = '.'
start_y += 1
end
end
end
result = 0
@updated_room.each_index do |y_coord|
@updated_room[y_coord].each_index do |x_coord|
result += x_coord + y_coord * 100 if @updated_room[y_coord][x_coord] == '['
end
end
puts result
# 1576353