-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdec5_1.rb
41 lines (33 loc) · 1.04 KB
/
dec5_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
require_relative 'advent_data'
data = AdventData.new(day: 5, session: ARGV[0]).get
diagram = [[]]
def prepare_coordinates(data)
data.map do |line|
first_pair, second_pair = line.split(' -> ')
x1, y1 = first_pair.split(',')
x2, y2 = second_pair.split(',')
{ x1: x1.to_i, y1: y1.to_i, x2: x2.to_i, y2: y2.to_i }
end
end
def mark_vents(coordinates, diagram)
coordinates.each do |set|
next unless set[:x1] == set[:x2] || set[:y1] == set[:y2]
if set[:x1] == set[:x2]
y1y2 = [set[:y1], set[:y2]]
for y in y1y2.min..y1y2.max do
diagram[y] = [] if diagram[y].nil?
diagram[y][set[:x1]] = diagram[y][set[:x1]].to_i + 1
end
else
x1x2 = [set[:x1], set[:x2]]
for x in x1x2.min..x1x2.max do
diagram[set[:y1]] = [] if diagram[set[:y1]].nil?
diagram[set[:y1]][x] = diagram[set[:y1]][x].to_i + 1
end
end
end
diagram
end
coordinates = prepare_coordinates(data)
diagram = mark_vents(coordinates, diagram)
puts diagram.flatten.select { |point| point.to_i > 1 }.count