Compare commits
3 Commits
e6a76e428f
...
fde761d6af
Author | SHA1 | Date | |
---|---|---|---|
fde761d6af | |||
24c93c7a63 | |||
a24b176417 |
22
common.cr
22
common.cr
|
@ -20,3 +20,25 @@ class Rectangle
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
class String
|
||||
def select(&block)
|
||||
String.build do |s|
|
||||
each_char do |c|
|
||||
s << c if yield c
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def select!(&block)
|
||||
i = 0
|
||||
s = size
|
||||
while i < s
|
||||
if yield [i]
|
||||
i += 1
|
||||
else
|
||||
s -= 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
34
day5.cr
Normal file
34
day5.cr
Normal file
|
@ -0,0 +1,34 @@
|
|||
require "./common.cr"
|
||||
|
||||
lines = File.read("day5_input").split("\n")
|
||||
|
||||
struct Char
|
||||
def inverse?(other)
|
||||
return (other != self) && (other.downcase == self.downcase)
|
||||
end
|
||||
end
|
||||
|
||||
def react(string)
|
||||
stack = [] of Char
|
||||
|
||||
string.each_char do |char|
|
||||
last_char = stack.last?
|
||||
if !last_char || !last_char.inverse? char
|
||||
stack << char
|
||||
else
|
||||
stack.pop
|
||||
end
|
||||
end
|
||||
|
||||
return stack
|
||||
end
|
||||
|
||||
puts react(lines[0]).size
|
||||
|
||||
pairs = {} of Tuple(Char, Char) => Int32
|
||||
('a'..'z').to_a.zip(('A'..'Z').to_a).each do |pair|
|
||||
new_string = lines[0].select { |c| c != pair[0] && c != pair[1] }
|
||||
pairs[pair] = react(new_string).size
|
||||
end
|
||||
|
||||
puts pairs.values.min
|
Loading…
Reference in New Issue
Block a user