blob: 562629f1cecc7faec4d9b36e4bb3b4df9ad162b9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
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
disc = 70_000_000
updt = 30_000_000
free = disc - root.size
need = updt - free
dirs = [root]
bigenough = []
until dirs.empty? do
this = dirs.shift
bigenough.push(this) if this.size >= need
dirs.push(*this.subdirs)
end
puts bigenough.map(&:size).min
|