aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-03-05 11:49:41 +0000
committerGitHub <noreply@github.com>2020-03-05 11:49:41 +0000
commit10f630aeee5db52e901c0d3e868a5104cd991852 (patch)
tree06ac2d76bcaebf34efc44f48678412492a42f2f9
parentc5a24234031519c316c2270b93aff2e562c9c53e (diff)
parenta7f00d154e8da2292e87aaf2dcb08a32b55c135f (diff)
downloadperlweeklychallenge-club-10f630aeee5db52e901c0d3e868a5104cd991852.tar.gz
perlweeklychallenge-club-10f630aeee5db52e901c0d3e868a5104cd991852.tar.bz2
perlweeklychallenge-club-10f630aeee5db52e901c0d3e868a5104cd991852.zip
Merge pull request #1358 from choroba/ech050a
Use enum instead of bitmask literals, remove the nested hash
-rwxr-xr-xchallenge-050/e-choroba/perl/ch-1.pl20
1 files changed, 10 insertions, 10 deletions
diff --git a/challenge-050/e-choroba/perl/ch-1.pl b/challenge-050/e-choroba/perl/ch-1.pl
index ccd05e335b..436d44f96c 100755
--- a/challenge-050/e-choroba/perl/ch-1.pl
+++ b/challenge-050/e-choroba/perl/ch-1.pl
@@ -4,27 +4,27 @@ use strict;
{ package MyInterval;
+ use enum 'BITMASK:' => qw( LEFT RIGHT SINGLE );
+
sub new { bless {}, shift }
sub insert {
my ($self, $from, $to) = @_;
- $self->{$from} ||= 0, undef $self->{same}{$from}, return
- if $from == $to;
- $self->{$from} |= 1;
- $self->{$_} = 3 for $from + 1 .. $to - 1;
- $self->{$to} |= 2;
+ $self->{$from} |= SINGLE, return if $from == $to;
+ $self->{$from} |= LEFT;
+ $self->{$_} = LEFT | RIGHT for $from + 1 .. $to - 1;
+ $self->{$to} |= RIGHT;
}
sub out {
my ($self) = @_;
my @r;
- my $same = $self->{same};
- for my $k (sort { $a <=> $b } grep $_ ne 'same', keys %$self) {
- if ($self->{$k} == 1) {
+ for my $k (sort { $a <=> $b } grep 'same' ne $_, keys %$self) {
+ if (($self->{$k} & (LEFT | RIGHT)) == LEFT) {
push @r, [$k];
- } elsif ($self->{$k} == 2) {
+ } elsif (($self->{$k} & (LEFT | RIGHT)) == RIGHT) {
push @{ $r[-1] }, $k
- } elsif ((! @r || 1 != @{ $r[-1] }) && exists $same->{$k}) {
+ } elsif ((! @r || 1 != @{ $r[-1] }) && ($self->{$k} == SINGLE)) {
push @r, [$k, $k];
}
}