-
Notifications
You must be signed in to change notification settings - Fork 1
/
14-2.rb
executable file
·172 lines (149 loc) · 2.96 KB
/
14-2.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
#!/usr/bin/env ruby
require 'numo/narray'
class Dish
attr_accessor :map
def initialize(input)
@height = input.size
@width = input[0].size
@map = Numo::UInt8.zeros(@height, @width)
@height.times do |y|
@width.times do |x|
@map[y, x] = 1 if input[y][x] == 'O'
@map[y, x] = 2 if input[y][x] == '#'
end
end
end
def load
load = 0
@height.times do |y|
@width.times do |x|
if @map[y, x] == 1
load += @height - y
end
end
end
load
end
def move_north!(start_y, x)
end_y = start_y
(start_y - 1).downto(0) do |check_y|
break unless map[check_y, x].zero?
end_y = check_y
end
map[start_y, x] = 0
map[end_y, x] = 1
end_y
end
def move_south!(start_y, x)
end_y = start_y
(start_y + 1).upto(@height - 1) do |check_y|
break unless map[check_y, x].zero?
end_y = check_y
end
map[start_y, x] = 0
map[end_y, x] = 1
end_y
end
def move_west!(y, start_x)
end_x = start_x
(start_x - 1).downto(0) do |check_x|
break unless map[y, check_x].zero?
end_x = check_x
end
map[y, start_x] = 0
map[y, end_x] = 1
end_x
end
def move_east!(y, start_x)
end_x = start_x
(start_x + 1).upto(@width - 1) do |check_x|
break unless map[y, check_x].zero?
end_x = check_x
end
map[y, start_x] = 0
map[y, end_x] = 1
end_x
end
def tilt_north!
@height.times do |y|
@width.times do |x|
if @map[y, x] == 1
move_north!(y, x)
end
end
end
end
def tilt_south!
(@height - 1).downto(0) do |y|
@width.times do |x|
if @map[y, x] == 1
move_south!(y, x)
end
end
end
end
def tilt_west!
@height.times do |y|
@width.times do |x|
if @map[y, x] == 1
move_west!(y, x)
end
end
end
end
def tilt_east!
@height.times do |y|
(@width - 1).downto(0) do |x|
if @map[y, x] == 1
move_east!(y, x)
end
end
end
end
def cycle!
tilt_north!
tilt_west!
tilt_south!
tilt_east!
end
def spin!
seen = []
num_cycles = 0
cycle_length = 0
loop do
seen << @map.copy
cycle!
num_cycles += 1
if seen.index @map
cycle_length = num_cycles - seen.index(@map)
break
end
end
_skip_cycles, remainder = (1_000_000_000 - num_cycles).divmod(cycle_length)
remainder.times { |_| cycle! }
end
def inspect
to_s
end
def to_s
s = "<#{self.class}:\n"
@height.times do |y|
@width.times do |x|
s += case @map[y, x]
when 0
'.'
when 1
'O'
when 2
'#'
end
end
s += "\n"
end
s += ">"
end
end
input = File.read('14.input').lines.map(&:strip).map(&:chars)
dish = Dish.new(input)
dish.spin!
print dish.load, "\n"