Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $inputFile = __DIR__.'/input.txt';
- $lines = file($inputFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
- $currentPath = null;
- $files= [];
- $dirs = [];
- foreach ($lines as $line) {
- if (substr($line, 0, 1) === '$') {
- if (substr($line, 0, 5) === '$ cd ') {
- $cd = substr($line, 5);
- if ($cd === '/') {
- $currentPath = '/';
- } elseif ($cd === '..') {
- $currentPath = dirname($currentPath);
- if ($currentPath !== '/') {
- $currentPath .= '/';
- }
- } else {
- $currentPath .= $cd.'/';
- }
- }
- } else {
- list($type, $name) = explode(' ', $line);
- if ($type !== 'dir') {
- $files[$currentPath][$currentPath.$name] = $type;
- }
- }
- }
- // Compute dir total sizes
- $dirSizes = [];
- foreach (array_keys($files) as $dir) {
- $dirSize = 0;
- foreach ($files as $dir2 => $dirFiles) {
- if (strpos($dir2, $dir) !== 0) {
- continue;
- }
- $dirSize += array_sum($dirFiles);
- }
- $dirSizes[$dir] = $dirSize;
- }
- // Part 1
- $part1 = 0;
- foreach ($dirSizes as $dir => $size) {
- if ($size <= 100000) {
- $part1 += $size;
- }
- }
- var_dump($part1);
Advertisement
Add Comment
Please, Sign In to add comment