-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path11-1.rb
executable file
·122 lines (108 loc) · 2.5 KB
/
11-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
#!/usr/bin/env ruby
require 'numo/narray'
class Universe
attr_accessor :space, :height, :width, :galaxies
def initialize(input)
@height = input.size
@width = input[0].size
@space = Numo::UInt8.zeros(@width, @height)
@height.times do |y|
@width.times do |x|
@space[y, x] = 1 if input[y][x] == '#'
end
end
@galaxies = nil
end
def expand!
expand_y = []
expand_x = []
@height.times do |y|
empty = true
@width.times do |x|
if @space[y, x] == 1
empty = false
break
end
end
expand_y << y if empty
end
@width.times do |x|
empty = true
@height.times do |y|
if @space[y, x] == 1
empty = false
break
end
end
expand_x << x if empty
end
expanded_height = @height + expand_y.size
expanded_space = Numo::UInt8.zeros(expanded_height, @width)
height_expansions = 0
@height.times do |y|
@width.times do |x|
expanded_space[height_expansions + y, x] =
@space[y, x]
end
if expand_y.include? y
@width.times do |x|
expanded_space[height_expansions + y + 1, x] = 0
end
height_expansions += 1
end
end
@space = expanded_space
@height = expanded_height
expanded_width = @width + expand_x.size
expanded_space = Numo::UInt8.zeros(expanded_height, expanded_width)
width_expansions = 0
@width.times do |x|
@height.times do |y|
expanded_space[y, width_expansions + x] =
@space[y, x]
end
if expand_x.include? x
@height.times do |y|
expanded_space[y, width_expansions + x + 1] = 0
end
width_expansions += 1
end
end
@space = expanded_space
@width = expanded_width
end
def find_galaxies!
@galaxies = []
@height.times do |y|
@width.times do |x|
@galaxies << [y, x] if space[y, x] == 1
end
end
end
def distances
distance = 0
@galaxies.permutation(2).map do |pair|
distance += (pair[0][0] - pair[1][0]).abs +
(pair[0][1] - pair[1][1]).abs
end
distance / 2
end
def inspect
to_s
end
def to_s
s = "<#{self.class}:\n"
@height.times do |y|
@width.times do |x|
s += @space[y, x].to_s
end
s += "\n"
end
s += ">"
end
end
input = File.read('11.input').lines.map(&:strip).map(&:chars)
u = Universe.new input
u.expand!
u.find_galaxies!
print u.distances, "\n"