-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.rb
More file actions
67 lines (47 loc) · 1 KB
/
5.rb
File metadata and controls
67 lines (47 loc) · 1 KB
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
require 'pry'
input = File.read('./5.input')
lowers = 'a'..'z'
uppers = 'A'..'Z'
pairs = (uppers.zip(lowers) + lowers.zip(uppers)).map &:join
PAIRS_REGEX = Regexp.new(pairs.join("|"))
class CharEater
def initialize
@gut = ""
end
def feed(char)
if !opposite_case?(@gut[-1], char)
@gut << char
end
end
def to_s
@gut
end
private
def opposite_case?(a, b)
return false if a == nil || b == nil
return false if a == b
a.downcase == b.downcase
end
end
def remove_opposite_cased_pairs(input)
input.gsub(PAIRS_REGEX, "")
end
def opposite_case?(a, b)
return false if a == b
a.downcase == b.downcase
end
def process_repeatedly(input)
until remove_opposite_cased_pairs(input) == input
input = remove_opposite_cased_pairs(input)
puts input.length
end
input
end
def test_removal
input = "abBcdEe"
output = input.gsub(PAIRS_REGEX, "")
if output != "acd"
raise "expected 'acd'. got '#{output}'"
end
end
puts process_repeatedly(input).length