blob: da3bd90260a4f251abd4bcd6cce895ebbed3ca2e (
plain) (
blame)
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
|
# Day 7: No Space Left On Device
import std/[os, strutils]
let input = paramStr(1).readFile().strip().split("\n")
type File = object
id: string
size: int
type Dir = ref object
id: string
files: seq[File]
dirs: seq[Dir]
parent: Dir
size: int
var root = Dir(id: "/")
var current = root
for line in input[1..^1]:
let output = line.split(" ")
if output.len == 2:
if output[0] == "$":
discard
elif output[0] == "dir":
current.dirs.add(Dir(id: output[1], parent: current))
else:
current.files.add(File(id: output[1], size: output[0].parseInt()))
else:
assert output[1] == "cd"
if output[2] == "..":
current = current.parent
else:
for i in current.dirs:
if i.id == output[2]:
current = i
break
proc update_size(a: Dir) =
for i in a.files:
a.size += i.size
for i in a.dirs:
i.update_size()
a.size += i.size
root.update_size()
const maximum_size = 100000
var sum = 0
proc calc_size(a: Dir) =
if a.size <= maximum_size:
sum += a.size
for i in a.dirs:
i.calc_size()
root.calc_size()
echo sum
const total_space = 70000000
const update_space = 30000000
let needed_space = update_space - (total_space - root.size)
var min = update_space
proc calc_min(a: Dir) =
if a.size >= needed_space:
min = min(a.size, min)
for i in a.dirs:
i.calc_min()
root.calc_min()
echo min
|