-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5b.rb
More file actions
48 lines (34 loc) · 888 Bytes
/
5b.rb
File metadata and controls
48 lines (34 loc) · 888 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
37
38
39
40
41
42
43
44
45
46
47
require 'pry'
input = File.read('./5.input')
lowers = 'a'..'z'
uppers = 'A'..'Z'
PAIR_REGEXEN = (uppers.zip(lowers)).map {|pair| Regexp.new(pair.join("|") )}
pairs = (uppers.zip(lowers) + lowers.zip(uppers)).map &:join
ALL_PAIRS_REGEX = Regexp.new(pairs.join("|"))
def remove_opposite_cased_pairs(input)
input.gsub(ALL_PAIRS_REGEX, "")
end
def process_repeatedly(input)
until remove_opposite_cased_pairs(input) == input
input = remove_opposite_cased_pairs(input)
end
input
end
def test_removal
input = "abBcdEe"
output = remove_opposite_cased_pairs(input)
if output != "acd"
raise "expected 'acd'. got '#{output}'"
end
end
inputs = {}
PAIR_REGEXEN.each do |regex|
processed = input.gsub(regex, "")
inputs[regex] = processed
end
lengths = []
inputs.each do |_, input|
lengths << process_repeatedly(input).length
print '.'
end
puts lengths.min