Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
910 B
11
Indexable
Never
def solve
  dirs = {}
  current_dir = ''
  File.read('day7/input').split("\n$ cd ").each do |line|
    current_dir_keys = current_dir.split('/')

    case line
    when '..'
      current_dir_keys.pop
      current_dir = current_dir_keys.join('/')
    else
      line.gsub("$ ls\n", '').split("\n").each do |line|
        if line.split(' ').first.to_i.positive?
          dirs[current_dir] += line.split(' ').first.to_i
        elsif line.split(' ').first != 'dir'
          current_dir += "/#{line}"
          dirs[current_dir] = 0
        end
      end
    end
  end

  dirs.reverse_each do |k, v|
    k_arr = k.split('/')
    until k_arr.join('/') == '/root'
      k_arr.pop if k_arr.length > 2
      dirs[k_arr.join('/')] += v
    end
  end

  p dirs.values.filter { _1 < 100_000 }.sum

  needed_space = 30_000_000 - (70_000_000 - dirs['/root'])
  p dirs.values.filter { needed_space < _1 }.min
end

solve