-
Notifications
You must be signed in to change notification settings - Fork 1
/
day5.rb
92 lines (64 loc) · 1.52 KB
/
day5.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
82
83
84
85
86
87
88
89
90
91
require_relative 'helpers'
def nice?(string)
chars = string.split('')
letter_repeat = false
return false if string.scan(/a|e|i|o|u/).count < 3
chars.each_with_index do |char, i|
next if i == 0
if char == chars[i - 1]
letter_repeat = true
break
end
end
return false unless letter_repeat
return false if string.match(/ab|cd|pq|xy/)
true
end
def pairs_detected?(string)
pair_detected = false
string_split = string.split('')
# Detect pairs
string_split.each_with_index do |char, i|
next if i == 0
prev_char = string_split[i - 1]
pair = "#{prev_char}#{char}"
((i+1)..(string.length-1)).each do |j|
next_pair = "#{string_split[j]}#{string_split[j+1]}"
if pair == next_pair
pair_detected = true
break
end
end
end
pair_detected
end
def inbetweener_found?(string)
found = false
string_split = string.split('')
# Detect in between
string_split.each_with_index do |char, i|
next unless i >= 2
over_char = string_split[i - 2]
between_char = string_split[i - 1]
if char == over_char
found = true
break
end
end
found
end
def new_nice?(string)
return false unless pairs_detected?(string)
return false unless inbetweener_found?(string)
true
end
nice_count = 0
read_input_lines(5).each do |line|
if new_nice?(line)
nice_count += 1
end
end
puts "There are #{nice_count} nice strings"
puts inbetweener_found?("xyx")
puts inbetweener_found?("abcdefeghi")
puts inbetweener_found?("aaa")