From 631b2642b4eec1d835237d06f136264de3e93e58 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sat, 7 Dec 2019 09:01:40 -0800 Subject: [PATCH] [2019][ruby][7.a] --- 2019/ruby/computer.rb | 2 ++ 2019/ruby/day_07.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 2019/ruby/day_07.rb diff --git a/2019/ruby/computer.rb b/2019/ruby/computer.rb index 8c0d823..911ad23 100644 --- a/2019/ruby/computer.rb +++ b/2019/ruby/computer.rb @@ -1,5 +1,7 @@ # typed: strict +require "stringio" + require "sorbet-runtime" AnyIO = T.type_alias { T.any(IO, StringIO) } diff --git a/2019/ruby/day_07.rb b/2019/ruby/day_07.rb new file mode 100644 index 0000000..df56b57 --- /dev/null +++ b/2019/ruby/day_07.rb @@ -0,0 +1,12 @@ +require_relative "computer" + +program = ARGF.read + +puts (0..4).to_a.permutation.map {|phase_settings| + amplifiers = Array.new(5) { Computer.from(program) } + amplifiers.zip(phase_settings).inject(?0) {|i, (a, ps)| + output = StringIO.new + a.run(StringIO.new("#{ps}\n#{i}"), output) + output.string + }.to_i +}.max