aboutsummaryrefslogtreecommitdiff
path: root/day07/part1.rb
diff options
context:
space:
mode:
Diffstat (limited to 'day07/part1.rb')
-rw-r--r--day07/part1.rb71
1 files changed, 71 insertions, 0 deletions
diff --git a/day07/part1.rb b/day07/part1.rb
new file mode 100644
index 0000000..94d0043
--- /dev/null
+++ b/day07/part1.rb
@@ -0,0 +1,71 @@
+lines = $stdin.readlines.map(&:strip)
+
+class FSObj
+ attr_reader :name
+end
+
+class FSDir < FSObj
+ def initialize(name, parent = self)
+ @name = name
+ @parent = parent
+ @entries = []
+ end
+
+ attr_reader :parent
+
+ def add_entry(obj)
+ @entries.push(obj)
+ end
+
+ def find_entry(name)
+ @entries.select{|ent|ent.name == name}[0]
+ end
+
+ def subdirs
+ @entries.select{|ent|ent.class == FSDir}
+ end
+
+ def size
+ @entries.map(&:size).sum
+ end
+end
+
+class FSFile < FSObj
+ def initialize(name, size)
+ @name = name
+ @size = size
+ end
+
+ attr_reader :size
+end
+
+root = FSDir.new(?/)
+current = root
+
+lines.each do |line|
+ case line
+ when "$ ls"
+ next
+ when "$ cd /"
+ next
+ when "$ cd .."
+ current = current.parent
+ when /\A\$ cd (.+)\Z/
+ current = current.find_entry($1)
+ when /\Adir (.+)\Z/
+ current.add_entry(FSDir.new($1, current))
+ when /\A(\d+) (.+)\Z/
+ current.add_entry(FSFile.new($2, $1.to_i))
+ end
+end
+
+dirs = [root]
+ltht = []
+
+until dirs.empty? do
+ this = dirs.shift
+ ltht.push(this) if this.size < 100000
+ dirs.push(*this.subdirs)
+end
+
+puts ltht.map(&:size).sum