aboutsummaryrefslogtreecommitdiff
path: root/day07/part1
diff options
context:
space:
mode:
Diffstat (limited to 'day07/part1')
-rwxr-xr-xday07/part144
1 files changed, 44 insertions, 0 deletions
diff --git a/day07/part1 b/day07/part1
new file mode 100755
index 0000000..2ef3505
--- /dev/null
+++ b/day07/part1
@@ -0,0 +1,44 @@
+#!/usr/bin/env ruby
+
+require 'pp'
+
+class Array
+ def to_empty_hash
+ h = {}
+ self.each do |el|
+ h[el] = nil
+ end
+ return h
+ end
+end
+
+input = $stdin.readlines.map(&:chomp)
+
+hash = {}
+
+input.each do |line|
+ me, them = line.split(" -> ")
+ _, name, weight = me.match(/([a-z]+) \((\d+)\)/).to_a
+ children = them.nil? ? [] : them.split(", ")
+
+ hash[name.intern] = {
+ :weight => weight,
+ :remove => false,
+ :children => children.map(&:intern).to_empty_hash
+ }
+end
+
+hash.each do |key, value|
+ value[:children].each do |ckey, cvalue|
+ if cvalue.nil? then
+ value[:children][ckey] = hash[ckey].dup
+ hash[ckey][:remove] = true
+ end
+ end
+end
+
+hash = hash.reject { |key, value|
+ value[:remove]
+}
+
+puts hash.keys.first