From 3e1c3c2b7b4fc8bb7c7b148632bbbe04be1307b6 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Thu, 5 Dec 2019 21:16:12 -0800 Subject: [PATCH] [2019][ruby][6.x] --- 2019/ruby/day_06.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 2019/ruby/day_06.rb diff --git a/2019/ruby/day_06.rb b/2019/ruby/day_06.rb new file mode 100644 index 0000000..6173ff7 --- /dev/null +++ b/2019/ruby/day_06.rb @@ -0,0 +1,22 @@ +orbits = ARGF + .read.split("\n").map {|l| l.split(?)) } + .each.with_object(Hash.new {|h,k| h[k] = []}) {|(a, b), o| + o[a] << b + } + +roots = orbits.keys.select {|x| orbits.values.none? {|v| v.include?(x) }} + +depths = {} +queue = roots.map {|x| [x, []] } +until queue.empty? + x, a = queue.shift + depths[x] = a + a = a + [x] + queue.concat(orbits.fetch(x) { [] }.map {|y| [y, a] }) +end + +# p depths.values.map(&:size).sum + +you = depths.fetch("YOU") +san = depths.fetch("SAN") +p ((you + san) - (you & san)).size