Advertisement
Guest User

aoc22day07

a guest
Dec 7th, 2022
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. public class Main {
  2.  
  3.     public static void main(String[] args) throws IOException {
  4.  
  5.         // Reads input and loads into the ElfFileSystem using the
  6.         List<String> data = IOUtils.readInputFile("day07input");
  7.         ElfFileSystem fs = new ElfFileSystem(data);
  8.  
  9.         //  Generates a map with all the paths in the ElfFileSystem with its whole size
  10.         Map<Path, Integer> pathSize = fs.getElfFileSystemPaths().stream()
  11.                 .collect(Collectors.toMap(i -> i, fs::getPathContentSize));
  12.  
  13.         // --------- Part 1, my input answer 1350966 ---------
  14.         int part1 = pathSize.values().stream().filter(i -> i < 100000).mapToInt(i -> i).sum();
  15.         System.out.println("Part 1: " + part1);
  16.  
  17.         //  --------- Part 2, my input answer 6296435 ---------
  18.         int neededSpace = pathSize.values().stream()
  19.                 .sorted(Comparator.reverseOrder()).limit(1)
  20.                 .findFirst().orElse(-1) + 30000000 - 70000000;
  21.  
  22.         int part2 = pathSize.values().stream().filter(i -> i >= neededSpace)
  23.                 .mapToInt(i -> i)
  24.                 .sorted()
  25.                 .limit(1).findFirst().orElse(-1);
  26.         System.out.println("Part 2: " + part2);
  27.     }
  28. }
  29.  
  30. public class FSElement {
  31.  
  32.     private final int size;
  33.     private final Path path;
  34.  
  35.     public FSElement(String fileName, Path path) {
  36.         if (Character.isDigit(fileName.charAt(0))) {
  37.             this.size = Integer.parseInt(fileName.split(" ")[0]);
  38.         } else {
  39.             this.size = 0;
  40.         }
  41.         this.path = path;
  42.     }
  43.  
  44.     public long getSize() {
  45.         return this.size;
  46.     }
  47.  
  48.     public Path getPath() {
  49.         return this.path;
  50.     }
  51. }
  52.  
  53. public class ElfFileSystem {
  54.  
  55.     private final List<FSElement> elements;
  56.     private Path currentPath;
  57.  
  58.     public ElfFileSystem(List<String> data) {
  59.         currentPath = Path.of(File.separator);
  60.         elements = new ArrayList<>();
  61.         for (String line : data) {
  62.             if (line.startsWith("$ cd")) {
  63.                 setPath(line.split(" ")[2]);
  64.             } else if (!line.startsWith("$ ls")) {
  65.                 elements.add(new FSElement(line, currentPath));
  66.             }
  67.         }
  68.     }
  69.  
  70.     private void setPath(String goTo) {
  71.         if (goTo.equals("..")) {
  72.             currentPath = currentPath.getParent();
  73.         } else if (goTo.equals("/")) {
  74.             currentPath = Path.of(File.separator);
  75.         } else {
  76.             currentPath = Path.of(currentPath.toString() + File.separator + goTo);
  77.         }
  78.     }
  79.  
  80.     public int getPathContentSize(Path path) {
  81.         int size = 0;
  82.         for (FSElement element : elements) {
  83.             if (element.getPath().toString().contains(path.toString())) {
  84.                 size += element.getSize();
  85.             }
  86.         }
  87.         return size;
  88.     }
  89.  
  90.     public Set<Path> getElfFileSystemPaths() {
  91.         return elements.stream().map(FSElement::getPath).collect(Collectors.toSet());
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement