Something went wrong. Try again.
Code for the Advent of Code event
aoc advent-of-code
Something went wrong. Try again.
700 B · 37 lines
Ruby
at rust
1234567891011121314151617181920212223242526272829303132333435363738#!/usr/bin/env ruby# frozen_string_literal: true
files = Hash.new(0)
cwd = []
ARGF.readlines.map(&:chomp).each do |line| case line when /^\$ cd \/$/ cwd.clear when /^\$ cd \.\.$/ cwd.pop when /^\$ cd (.+)/ cwd.push $1 when /^(\d+) (.+)$/ files["/#{cwd.join ?/}#{cwd.empty? ? '' : '/'}#{$2}"] = $1.to_i endend
directories = Hash.new(0)
def record_size(h, path, size) dir = File.dirname(path) h[dir] += size
return if dir == '/'
record_size h, dir, sizeend
files.each { |n, s| record_size directories, n, s }
puts directories.map { _2 }.select { _1 <= 100_000 }.sum
min = directories[?/] - 40_000_000puts directories.map { _2 }.sort.find { _1 >= min }