Skip to content

Instantly share code, notes, and snippets.

@ynonp
Created November 6, 2012 07:47
Show Gist options
  • Save ynonp/4023291 to your computer and use it in GitHub Desktop.
Save ynonp/4023291 to your computer and use it in GitHub Desktop.
solution for 5
A: B
B: C, D
E: D
use strict;
use warnings;
use v5.14;
my %deps;
# A line looks like this:
# module: dep1, dep2, dep3
#
# We add it to the hash as an array ref
# A: B
# B: C, D
# C: D
sub insert_line_to_hash {
my ($line) = @_;
my ( $module, $deps ) = split /\s*:\s*/, $line;
my ( @deps ) = split /\s*,\s*/, $deps;
$deps{$module} = \@deps;
foreach my $m (@deps) {
if ( ! exists $deps{$m} ) {
$deps{$m} = [];
}
}
}
sub get_next_product {
my $result;
while ( my ($k, $v ) = each %deps ) {
# found it
if ( @$v == 0 ) {
$result = $k;
}
}
# returns false
return $result;
}
sub build_product {
my ($product) = @_;
say $product;
# delete from hash
delete $deps{$product};
# delete from others who depend on it
while ( my ($k, $v) = each %deps ) {
$deps{$k} = [ grep { $_ ne $product } @$v ];
}
}
open my $fh, '<', 'deps';
while (<$fh>) {
chomp;
insert_line_to_hash($_);
}
close $fh;
while ( my $next = get_next_product() ) {
build_product( $next );
}
use Data::Dumper;
print Dumper( \%deps );
use strict;
use warnings;
use File::Find;
use Storable;
use v5.14;
my $counter = 0;
my $prev_counter = 0;
eval {
my $info_ref = retrieve('counter.bin');
$prev_counter = $info_ref->{counter};
};
sub count_txt {
$counter++ if /txt$/;
}
find(\&count_txt, '.');
say "total: $counter";
say "diff: ", $counter - $prev_counter;
store ({counter => $counter}, 'counter.bin');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment