Untitled
unknown
d
3 years ago
2.6 kB
17
Indexable
import std.stdio; import std.array; import std.conv; import std.format; import std.algorithm; import std.algorithm.iteration; class Node{ protected string name; this(string name){ this.name = name; } public abstract int sum(); abstract Dir[] find_dir(int condition); string getName(){ return name; } } class FileN: Node{ Dir parent; int size; this(Dir parent, string name, int size){ super(name); this.parent = parent; this.size = size; } override int sum(){ return size; } override Dir[] find_dir(int condition){ return []; } } class Dir: Node{ Dir parent; Node[string] childs; this(Dir parent, string name){ super(name); this.parent = parent; this.parent.add(this); } this(string name){ super(name); this.parent = null; } public void add(Node node){ childs[node.name] = node; } override int sum(){ int size = 0; foreach(node; childs){ size += node.sum(); } return size; } override Dir[] find_dir(int condition){ Dir[] res; foreach(node; childs){ res ~= node.find_dir(condition); } if (sum() > condition){ res ~= this; } return res; } } void main() { File input = File("../input7", "r"); string line; Dir root = null; Dir where = null; int control = 0; while ((line = input.readln()) !is null){ line = line.replace("\n", ""); auto attrs = line.split(" "); if (attrs[0] == "$"){ // une commande if (attrs[1] == "cd"){ if (root is null){ root = new Dir("/"); where = root; } else if (attrs[2] == ".."){ where = where.parent; } else{ Dir aDir = new Dir(where, attrs[2]); where = aDir; } } } else if (attrs[0] == "dir"){ // une définition de dossier //ignore }else{ // un fichier FileN aFile = new FileN(where, attrs[1], to!(int)(attrs[0])); where.add(aFile); control+=to!(int)(attrs[0]); } } int free_space = 70000000 - root.sum(); int tofree = 30000000 - free_space; Dir[] dirs = root.find_dir(tofree); sort!("a.sum()>b.sum()")(dirs); foreach(dir; dirs){ writeln(format("Dir %s, size %d", dir.name, dir.sum())); } }
Editor is loading...