-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05a_solution.rb
38 lines (33 loc) · 902 Bytes
/
05a_solution.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
input = File.read("./05input.txt").split("\n")
vents = input.map do |line|
origin, endpoint = line.split(" -> ").map do |point|
coords = point.split(",").map(&:to_i)
{ x: coords[0], y: coords[1] }
end
{ from: origin, to: endpoint }
end
lines_crossing = Hash.new { |h, k| h[k] = 0 }
# range ignoring order (normally, 4..1 is an empty range, we want it to return 1..4)
def range(a, b)
if a > b
b..a
else
a..b
end
end
vents.each do |vent|
if vent[:from][:x] == vent[:to][:x]
x = vent[:from][:x]
range(vent[:from][:y], vent[:to][:y]).each do |y|
point = { x: x, y: y }
lines_crossing[point] += 1
end
elsif vent[:from][:y] == vent[:to][:y]
y = vent[:from][:y]
range(vent[:from][:x], vent[:to][:x]).each do |x|
point = { x: x, y: y }
lines_crossing[point] += 1
end
end
end
puts lines_crossing.values.count { |c| c >= 2 }