-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4_2.rb
More file actions
36 lines (27 loc) · 834 Bytes
/
day4_2.rb
File metadata and controls
36 lines (27 loc) · 834 Bytes
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
class Jobs
def initialize(input)
@overlapped_pairs = 0
input_file(input)
end
attr_reader :overlapped_pairs
def input_file(input)
File.foreach(input) do |line|
one_tasks = get_tasks(line, 0)
two_tasks = get_tasks(line, 1)
if overlapped_pair?(one_tasks, two_tasks)
@overlapped_pairs += 1
end
end
end
def get_tasks(line, elf)
elfs_tasks_string = line.split(",")[elf]
elfs_tasks = elfs_tasks_string.split("-").map{|a| a.to_i}.to_a
end
def overlapped_pair?(elf1, elf2)
r1 = Range.new(elf1[0], elf1[1], false)
r2 = Range.new(elf2[0], elf2[1], false)
return r2.begin <= r1.end && r1.begin <= r2.end
end
end
today = Jobs.new("day4i")
puts today.overlapped_pairs