From: Jan "Yenya" Kasprzak Date: Wed, 7 Dec 2022 05:19:20 +0000 (+0100) Subject: Day 7: nice! X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=01199d84c228b2af2dc9e5f2c1ee65e07ae5ccdb;p=aoc.git Day 7: nice! --- diff --git a/2022/13.pl b/2022/13.pl new file mode 100755 index 0000000..23b0ce9 --- /dev/null +++ b/2022/13.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl -w + +use v5.36; +use strict; +use experimental 'multidimensional'; +use List::Util qw(sum); + +my @path = ''; +my %files; +my %dirs = ('' => 0); + +while (<>) { + chomp; + if (/^\$ cd \/$/) { + @path = ''; + } elsif (/^\$ cd \.\.$/) { + pop @path; + } elsif (/^\$ cd (.*)/) { + push @path, $1; + } elsif (/^\$ ls/) { + } elsif (/^dir (.*)/) { + $dirs{join('/', @path, $1)} = 0; + } elsif (/^(\d+) (.*)/) { + my $file = join('/', @path, $2); + $files{$file} = $1; + } +} + +for my $dir (keys %dirs) { + for my $file (keys %files) { + $dirs{$dir} += $files{$file} if $file =~ /^$dir\//; + } +} + +say sum map { $dirs{$_} >= 100_000 ? 0 : $dirs{$_} } keys %dirs; + + + diff --git a/2022/14.pl b/2022/14.pl new file mode 100755 index 0000000..f67a953 --- /dev/null +++ b/2022/14.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl -w + +use v5.36; +use strict; +use experimental 'multidimensional'; +use List::Util qw(sum); + +my @path = ''; +my %files; +my %dirs = ('' => 0); + +while (<>) { + chomp; + if (/^\$ cd \/$/) { + @path = ''; + } elsif (/^\$ cd \.\.$/) { + pop @path; + } elsif (/^\$ cd (.*)/) { + push @path, $1; + } elsif (/^\$ ls/) { + } elsif (/^dir (.*)/) { + $dirs{join('/', @path, $1)} = 0; + } elsif (/^(\d+) (.*)/) { + my $file = join('/', @path, $2); + $files{$file} = $1; + } +} + +for my $dir (keys %dirs) { + for my $file (keys %files) { + $dirs{$dir} += $files{$file} if $file =~ /^$dir\//; + } +} + +my $reqd = $dirs{''} - 40000000; + +for my $dir (sort { $dirs{$a} <=> $dirs{$b} } keys %dirs) { + if ($dirs{$dir} >= $reqd) { + say $dir, ' ', $dirs{$dir}; + last; + } +} +