Advertisement
FubFubFub

Day 7

Dec 7th, 2022
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 1.16 KB | Source Code | 0 0
  1. % The root of the filesystem
  2. root(root).
  3.  
  4. % A directory can have a sub-directory
  5. sub_directory(root, a).
  6. sub_directory(root, d).
  7. sub_directory(a, e).
  8.  
  9. % We don't need to know the file name, so we record only the file sizes
  10. file(root, 14848514).
  11. file(root, 8504156).
  12. file(a, 29116).
  13. file(a, 2557).
  14. file(a, 62596).
  15. file(e, 584).
  16. file(d, 4060174).
  17. file(d, 8033020).
  18. file(d, 5626152).
  19. file(d, 7214296).
  20.  
  21. % The root is a directory. An subdirectory of a directory is a directory itself.
  22. directory(X) :- root(X).
  23. directory(X) :- sub_directory(Y, X), directory(Y).
  24.  
  25. % The size of a directory is the size of all files in the directory plus the sizes of all subdirectories
  26. 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.
  27.  
  28. % Find all directories with a size at most 100000 and sum their sizes
  29. 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