-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.rb
81 lines (70 loc) · 1.64 KB
/
day4.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'byebug'
class CleanUp
def initialize(input)
@input = input
@total = 0
end
def num_of_pairs_duplicated
@input.split("\n").each do |pair|
p top = pair.split(",")[0]
p bottom = pair.split(",")[1]
top_start = top.split("-")[0].to_i
top_end = top.split("-")[1].to_i
bottom_start = bottom.split("-")[0].to_i
bottom_end = bottom.split("-")[1].to_i
if top_start <= bottom_start && top_end >= bottom_end
@total += 1
end
if bottom_start <= top_start && bottom_end >= top_end
@total += 1
end
end
p @total
end
def duped_pairs
count = 0
@input.split("\n").each do |pair|
top = Pair.new(pair.split(",")[0])
bottom = Pair.new(pair.split(",")[1])
#3-7,4-8
# if top.start <= bottom.start && top.end >= bottom.end
# count += 1
# elsif bottom.start <= top.start && bottom.end >= top.end
# count += 1
# else
# next
# end
count += 1 if common_number?(top, bottom)
end
count
end
def common_number?(top, bottom)
(top.full_set & bottom.full_set) != []
end
def find_range(coordinates)
coor_array = coordinates.split("-")
start = coor_array[0].to_i
end_p = coor_array[1].to_i
(start..end_p).to_a.join(",")
end
class Pair
def initialize(pair)
@start = pair.split("-")[0].to_i
@end = pair.split("-")[1].to_i
end
def start
@start
end
def end
@end
end
def range
@end - @start
end
def full_set
(@start..@end).to_a
end
end
end
c = CleanUp.new(File.read('day4input.txt'))
p c.duped_pairs