This commit is contained in:
Yanick Champoux 2018-03-02 13:54:15 -05:00
parent a4f4f9ed3b
commit 62e20d3282
3 changed files with 1122 additions and 0 deletions

46
2016/20/1.pl Normal file
View File

@ -0,0 +1,46 @@
use 5.20.0;
my @range = ( [ 0, 4294967295 ] );
while(<>) {
my $avoid = [ split '-' ];
@range = map { remove_range($_, $avoid) } @range;
}
use DDP;
p $range[0];
use experimental qw/ signatures /;
use List::AllUtils qw/ min max minmax /;
sub remove_range( $range, $avoid ) {
return $range if $avoid->[0] > $range->[1]
or $avoid->[1] < $range->[0];
return ()
if $avoid->[0] <= $range->[0]
and $avoid->[1] >= $range->[1];
my @range;
if( $avoid->[0] <= $range->[0] ) {
@range = ( [ $avoid->[1]+1, $range->[1] ] );
}
elsif( $avoid->[1] >= $range->[1] ) {
@range = ( [ $range->[0], $avoid->[0]-1] );
}
else {
@range = (
[ $range->[0], $avoid->[0]-1],
[ $avoid->[1]+1, $range->[1] ] );
}
return grep { $_->[0] <= $_->[1] } @range;
}

47
2016/20/2.pl Normal file
View File

@ -0,0 +1,47 @@
use 5.20.0;
my @range = ( [ 0, 4294967295 ] );
while(<>) {
my $avoid = [ split '-' ];
@range = map { remove_range($_, $avoid) } @range;
}
use experimental qw/ signatures /;
use List::AllUtils qw/ min max minmax sum /;
use DDP;
say sum map { $_->[1] - $_->[0] + 1 } @range;
sub remove_range( $range, $avoid ) {
return $range if $avoid->[0] > $range->[1]
or $avoid->[1] < $range->[0];
return ()
if $avoid->[0] <= $range->[0]
and $avoid->[1] >= $range->[1];
my @range;
if( $avoid->[0] <= $range->[0] ) {
@range = ( [ $avoid->[1]+1, $range->[1] ] );
}
elsif( $avoid->[1] >= $range->[1] ) {
@range = ( [ $range->[0], $avoid->[0]-1] );
}
else {
@range = (
[ $range->[0], $avoid->[0]-1],
[ $avoid->[1]+1, $range->[1] ] );
}
return grep { $_->[0] <= $_->[1] } @range;
}

1029
2016/20/input.txt Normal file

File diff suppressed because it is too large Load Diff