-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.rb
60 lines (49 loc) · 1.02 KB
/
7.rb
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
#!/usr/bin/env ruby
class Fs
attr_accessor :input
def initialize(input:)
@input = input
@pwd = []
@nodes = Hash.new { |h,k| h[k] = 0 }
end
def cd(path)
if path == '..'
@pwd.pop
else
@pwd << path
end
end
def node(name, size)
@pwd.size.times.each do |i|
path = @pwd[0..i].join("/")
@nodes[path] += size
end
end
def call
input.split('$').drop(1).map do |block|
command, *lines = block.lines
bin, arg = command.split
case bin
when "cd"
cd(arg)
when "ls"
lines.each do |line|
type, name = line.split
if type != "dir"
node(name, type.to_i)
end
end
end
end
part_1 = @nodes.values.select do |size|
size <= 100000
end.sum
space_needed = 30000000 - (70000000 - @nodes["/"])
part_2 = @nodes.values.keep_if do |size|
size >= space_needed
end.sort.first
puts part_1, part_2
end
end
input = ARGF.read
Fs.new(input: input).call