-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_07_1.rb
68 lines (59 loc) · 941 Bytes
/
day_07_1.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
61
62
63
64
65
66
67
68
data = '''$ cd /
$ ls
dir a
14848514 b.txt
8504156 c.dat
dir d
$ cd a
$ ls
dir e
29116 f
2557 g
62596 h.lst
$ cd e
$ ls
584 i
$ cd ..
$ cd ..
$ cd d
$ ls
4060174 j
8033020 d.log
5626152 d.ext
7214296 k'''
data = File.open("inp_7").read()
def get_size(k, dir)
sum = dir[k][:sum]
dir[k][:children].each do |c|
sum += get_size("#{k};#{c}", dir)
end
sum
end
cwd = []
dir = Hash.new { |h, k| h[k] = { :sum => 0, :children => [] } }
data.split("\n").each do |l|
x,y,z = l.split(" ")
case x
when "$"
next unless y == "cd"
if z == ".."
cwd.pop()
else
cwd.push(z)
end
when "dir"
dir[cwd.join(";")][:children] << y
else
dir[cwd.join(";")][:sum] += x.to_i
end
end
needed_space = 30000000 - (70000000 - get_size("/", dir))
potentials = []
total = 0
dir.each do |k,v|
s = get_size(k, dir)
total += s if s <= 100000
potentials << s if s > needed_space
end
puts "part 1: #{total}"
puts "part 2: #{potentials.min}"