From 2072187d1611da94d1cd9bcee24bc8198172f5a4 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Tue, 3 Dec 2019 07:32:16 -0800 Subject: [PATCH] [2019][ruby][3.a] --- 2019/ruby/day_03.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 2019/ruby/day_03.rb diff --git a/2019/ruby/day_03.rb b/2019/ruby/day_03.rb new file mode 100644 index 0000000..c99257a --- /dev/null +++ b/2019/ruby/day_03.rb @@ -0,0 +1,23 @@ +wire_paths = ARGF.read.lines.map {|l| + l.split(?,).map {|i| [i[0], i[1..-1].to_i] } +} + +wire_points = wire_paths.map {|path| + path.each.with_object([[0, 0]]) {|(dir, length), points| + x, y = points.last + length.times {|i| + points << case dir + when ?R then [x+i+1, y] + when ?U then [x, y+i+1] + when ?L then [x-i-1, y] + when ?D then [x, y-i-1] + end + } + } +} + +intersections = wire_points[0] & wire_points[1] +intersections.delete([0, 0]) + +distances = intersections.map {|(x, y)| x.abs + y.abs } +puts distances.min