-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05b_solution.rb
47 lines (41 loc) · 1.13 KB
/
05b_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
39
40
41
42
43
44
45
46
47
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 with order (normally, 4..1 is an empty range, we want it to return [4, 3, 2, 1])
def range(a, b)
if a > b
(b..a).to_a.reverse
else
(a..b).to_a
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
else
# diagonal
points = range(vent[:from][:x], vent[:to][:x])
.zip(range(vent[:from][:y], vent[:to][:y]))
.map do |p|
{ x: p[0], y: p[1] }
end
points.each { |p| lines_crossing[p] += 1 }
end
end
puts lines_crossing.values.count { |c| c >= 2 }