Skip to content

Instantly share code, notes, and snippets.

@abhayathapa
Created January 30, 2023 05:42
Show Gist options
  • Save abhayathapa/755ef1d8a3413ec7e40292bc9bc25a1e to your computer and use it in GitHub Desktop.
Save abhayathapa/755ef1d8a3413ec7e40292bc9bc25a1e to your computer and use it in GitHub Desktop.
Solution for 99minds to find Sum of sizes of directories less than 100000
require 'open-uri'
input = URI.open("https://gist.githubusercontent.com/ronakjain90/3dcfef6a752f77b3e8202ad9844195ae/raw/599eaaeec9f47b70a44da3454a731fee639efff3/input.txt").readlines
current_directory = []
directories = Hash.new { |h, k| h[k] = 0 }
input
.map(&:chomp)
.map(&:split)
.each do |line|
case line
in ['$', 'cd', '..']
current_directory.pop
in ['$', 'cd', dir]
current_directory << dir
in [size, filename]
current_directory.length.times do |a|
directories[current_directory[0..a]] += size.to_i
end
end
end
result = directories.select { |h,size| size <= 100000 }
p 'All directories less than 100000'
p result.keys
p "Sum of sizes of directories less than 100000 -> #{result.values.sum}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment