Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % The root of the filesystem
- root(root).
- % A directory can have a sub-directory
- sub_directory(root, a).
- sub_directory(root, d).
- sub_directory(a, e).
- % We don't need to know the file name, so we record only the file sizes
- file(root, 14848514).
- file(root, 8504156).
- file(a, 29116).
- file(a, 2557).
- file(a, 62596).
- file(e, 584).
- file(d, 4060174).
- file(d, 8033020).
- file(d, 5626152).
- file(d, 7214296).
- % The root is a directory. An subdirectory of a directory is a directory itself.
- directory(X) :- root(X).
- directory(X) :- sub_directory(Y, X), directory(Y).
- % The size of a directory is the size of all files in the directory plus the sizes of all subdirectories
- directory_size(Dir, Size) :- findall(FileSize, file(Dir, FileSize), SizeList), sum_list(SizeList, DirectFiles), findall(SubDirSize, (sub_directory(Dir, SubDir), directory_size(SubDir, SubDirSize)), SubDirSizeList), sum_list(SubDirSizeList, SubDirs), Size is DirectFiles + SubDirs.
- % Find all directories with a size at most 100000 and sum their sizes
- solve(SizeSum) :- findall(DirSize, (directory(Dir), directory_size(Dir, DirSize), DirSize =< 100000), DirSizeList), sum_list(DirSizeList, SizeSum).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement