Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use v5.12;
- use warnings;
- use List::Util qw(sum min);
- use constant DISK_SIZE => 70_000_000;
- use constant NEED => 30_000_000;
- $/ = '$ '; # break input on cmd prompts
- # read input, throwing out the cmd prompts
- my @input = map { [grep { $_ ne '$ ' } split /\n/] } <>;
- shift( @input ); # empty, because $ is a prompt not a divider
- my %dirs; # directory path name => size
- my @path; # path of directories to cwd
- foreach (@input) {
- my ($cmd, @res) = @$_;
- if ($cmd =~ m#cd (.*)#) {
- if ($1 eq '/') {
- @path = ('/');
- } elsif ($1 eq '..') {
- pop( @path );
- } else {
- push( @path, $1 );
- }
- } else {
- # cmd is ls, parse result
- foreach (@res) {
- my ($type, $name) = m#(.*) (.*)#; # Bright eyes, Elven device!
- if ($type ne 'dir') {
- my $path;
- foreach my $lvl (@path) {
- $path .= "$lvl/";
- $dirs{ $path } += $type;
- }
- }
- }
- }
- }
- say "Part 1: ", sum grep { $_ <= 100_000 } values %dirs;
- my $needed = NEED - (DISK_SIZE - $dirs{'//'});
- say "Part 2: ", min grep { $_ >= $needed } values %dirs;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement