From 15953efb0f922db2008c9f49d416cc7ebd326c9b Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Wed, 5 Dec 2018 07:11:13 -0800 Subject: [PATCH] [2018][ruby][5.x] --- 2018/ruby/day_05.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 2018/ruby/day_05.rb diff --git a/2018/ruby/day_05.rb b/2018/ruby/day_05.rb new file mode 100644 index 0000000..859e8d6 --- /dev/null +++ b/2018/ruby/day_05.rb @@ -0,0 +1,29 @@ +input = ARGF.read.chomp + +REGEX = Regexp.new( + (?a..?z) + .map {|x| [x, x.upcase] } + .flat_map(&:permutation) + .flat_map(&:to_a) + .map(&:join) + .join(?|) +) + +def react(polymer) + last = nil + until polymer == last + last = polymer + polymer = polymer.gsub(REGEX, "") + end + polymer +end + +# Part One +# p react(input).size + +# Part Two +p (?a..?z) + .map {|unit| input.gsub(/#{unit}/i, "") } + .map {|polymer| react(polymer) } + .map {|reacted| reacted.size } + .min