Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e0dcf7c

Browse filesBrowse files
committed
Day 11 part 1 Ruby solution
1 parent d22be32 commit e0dcf7c
Copy full SHA for e0dcf7c

File tree

Expand file treeCollapse file tree

1 file changed

+122
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+122
-0
lines changed

‎11-1.rb

Copy file name to clipboard
+122Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'numo/narray'
4+
5+
class Universe
6+
attr_accessor :space, :height, :width, :galaxies
7+
8+
def initialize(input)
9+
@height = input.size
10+
@width = input[0].size
11+
@space = Numo::UInt8.zeros(@width, @height)
12+
@height.times do |y|
13+
@width.times do |x|
14+
@space[y, x] = 1 if input[y][x] == '#'
15+
end
16+
end
17+
@galaxies = nil
18+
end
19+
20+
def expand!
21+
expand_y = []
22+
expand_x = []
23+
24+
@height.times do |y|
25+
empty = true
26+
@width.times do |x|
27+
if @space[y, x] == 1
28+
empty = false
29+
break
30+
end
31+
end
32+
expand_y << y if empty
33+
end
34+
35+
@width.times do |x|
36+
empty = true
37+
@height.times do |y|
38+
if @space[y, x] == 1
39+
empty = false
40+
break
41+
end
42+
end
43+
expand_x << x if empty
44+
end
45+
46+
expanded_height = @height + expand_y.size
47+
expanded_space = Numo::UInt8.zeros(expanded_height, @width)
48+
height_expansions = 0
49+
@height.times do |y|
50+
@width.times do |x|
51+
expanded_space[height_expansions + y, x] =
52+
@space[y, x]
53+
end
54+
if expand_y.include? y
55+
@width.times do |x|
56+
expanded_space[height_expansions + y + 1, x] = 0
57+
end
58+
height_expansions += 1
59+
end
60+
end
61+
@space = expanded_space
62+
@height = expanded_height
63+
64+
expanded_width = @width + expand_x.size
65+
expanded_space = Numo::UInt8.zeros(expanded_height, expanded_width)
66+
width_expansions = 0
67+
@width.times do |x|
68+
@height.times do |y|
69+
expanded_space[y, width_expansions + x] =
70+
@space[y, x]
71+
end
72+
if expand_x.include? x
73+
@height.times do |y|
74+
expanded_space[y, width_expansions + x + 1] = 0
75+
end
76+
width_expansions += 1
77+
end
78+
end
79+
@space = expanded_space
80+
@width = expanded_width
81+
end
82+
83+
def find_galaxies!
84+
@galaxies = []
85+
@height.times do |y|
86+
@width.times do |x|
87+
@galaxies << [y, x] if space[y, x] == 1
88+
end
89+
end
90+
end
91+
92+
def distances
93+
distance = 0
94+
@galaxies.permutation(2).map do |pair|
95+
distance += (pair[0][0] - pair[1][0]).abs +
96+
(pair[0][1] - pair[1][1]).abs
97+
end
98+
distance / 2
99+
end
100+
101+
def inspect
102+
to_s
103+
end
104+
105+
def to_s
106+
s = "<#{self.class}:\n"
107+
@height.times do |y|
108+
@width.times do |x|
109+
s += @space[y, x].to_s
110+
end
111+
s += "\n"
112+
end
113+
s += ">"
114+
end
115+
end
116+
117+
input = File.read('11.input').lines.map(&:strip).map(&:chars)
118+
119+
u = Universe.new input
120+
u.expand!
121+
u.find_galaxies!
122+
print u.distances, "\n"

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.