-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17-1.rb
executable file
·189 lines (166 loc) · 3.58 KB
/
17-1.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
#!/usr/bin/env ruby
require 'numo/narray'
class Rock
@@shapes = [
[[1, 1, 1, 1]],
[[0, 1, 0],
[1, 1, 1],
[0, 1, 0]],
[[0, 0, 1],
[0, 0, 1],
[1, 1, 1]],
[[1],
[1],
[1],
[1]],
[[1, 1],
[1, 1]]
]
def self.shapes
@@shapes
end
def [](row, col)
@rock[row][col]
end
def width
@rock[0].size
end
def height
@rock.size
end
attr_accessor :x, :y
def initialize(shape)
@rock = @@shapes[shape]
@x = 2
@y = 0
end
end
class Cave
def initialize(jets)
@num_rocks = 0
@height = 25
@map = Numo::UInt8.zeros(@height, 7)
@next_shape = -1
@jets = jets.chars.map { |j| j == '<' ? -1 : +1 }
@jet_offset = -1
@rock = nil
@highest_rock = @height
end
attr_reader :num_rocks
def height
@height - @highest_rock
end
def step!
unless @rock
@next_shape = (@next_shape + 1) % Rock.shapes.size
@rock = Rock.new(@next_shape)
@rock.y = @highest_rock - 3 - @rock.height
expand! if @rock.y.negative?
end
push!
@rock = nil unless fall!
end
def push!
@jet_offset = (@jet_offset + 1) % @jets.size
dir = @jets[@jet_offset]
can_move = true
if @rock.x + dir < 0 or
@rock.width + @rock.x + dir > 7
can_move = false
else
@rock.height.times do |row|
@rock.width.times do |col|
next unless @rock[row, col] == 1
if @map[row + @rock.y, col + @rock.x + dir] != 0
can_move = false
break
end
end
end
end
@rock.x += dir if can_move
end
def fall!
can_fall = true
if @rock.height + @rock.y >= @height
can_fall = false
else
@rock.height.times do |row|
@rock.width.times do |col|
next unless @rock[row, col] == 1
if @map[row + @rock.y + 1, col + @rock.x] != 0
can_fall = false
break
end
end
end
end
if can_fall
@rock.y += 1
else
@rock.height.times do |row|
@rock.width.times do |col|
@map[row + @rock.y, col + @rock.x] = @rock[row, col]
end
end
@highest_rock = [@rock.y, @highest_rock].min
@num_rocks += 1
end
can_fall
end
def expand!
add = 100
z = Numo::UInt8.zeros(add, 7)
@map = z.append(@map, axis:0)
@highest_rock += add
@rock.y += add if @rock
@height += add
end
def to_s
s = "<#{self.class}:\n"
s += "Next shape: #{@next_shape}, next push: #{@jets[@jet_offset]}\n"
s += "Highest rock: #{@highest_rock}\n"
@height.times do |row|
s += row.to_s.rjust(Math::log10(@height).ceil, '0') + ' '
7.times do |col|
tile = 0
if @rock and
row.between?(@rock.y, @rock.y + @rock.height - 1) and
col.between?(@rock.x, @rock.x + @rock.width - 1) and
@rock[row - @rock.y, col - @rock.x] == 1
tile = 2
end
tile += @map[row, col]
s += case tile
when 0
'.'
when 1
'#'
when 2
'@'
else
'!'
end
end
s += "\n"
end
if @rock
s += "Rock: x: #{@rock.x}, y: #{@rock.y}\n"
@rock.height.times do |row|
@rock.width.times do |col|
s += @rock[row, col].zero? ? '.' : '@'
end
s += "\n"
end
s += ">"
end
s
end
alias inspect to_s
end
input = File.read('17.input').strip
cave = Cave.new(input)
until cave.num_rocks == 2022 do
cave.step!
end
print cave.height, "\n"