-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.cs
69 lines (64 loc) · 1.45 KB
/
7.cs
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
/*
$ 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
*/
Node root = new(null, "/"), current = root;
List<Node> directories = new() {root};
while (Console.ReadLine() is { } line)
{
switch (line.Split())
{
case ["$", "ls"]:
break;
case ["$", "cd", ".."]:
current = current.Parent!;
break;
case ["$", "cd", "/"]:
current = root;
break;
case ["$", "cd", var dir]:
current = current.Children[dir];
break;
case ["dir", var dir]:
current.Children.Add(dir, new Node(current, dir));
directories.Add(current.Children[dir]);
break;
case [var size, var name]:
current.Children.Add(name, new Node(current, name));
current.Children[name].AddSize(int.Parse(size));
break;
}
}
Console.WriteLine("P1: " + directories.Select(n => n.Size).Where(s => s < 100000).Sum());
Console.WriteLine("P2: " + directories.Select(n => n.Size).Where(s => s >= 30000000 - (70000000 - root.Size)).Min());
internal record Node(Node? Parent, string Name)
{
public int Size { get; private set; }
public Dictionary<string, Node> Children { get; } = new();
public void AddSize(int size)
{
Size += size;
Parent?.AddSize(size);
}
}