Skip to content

Instantly share code, notes, and snippets.

@rightfold

rightfold/.p6 Secret

Created October 19, 2015 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rightfold/e683ff5da9e4895ac7fb to your computer and use it in GitHub Desktop.
Save rightfold/e683ff5da9e4895ac7fb to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl6
constant FACTORS = do {
my %factors =
s => 1,
m => 60,
h => 3600,
d => 86400,
y => 31536000,
;
for %factors.keys.eager {
%factors{"Y$_"} = 1000000000000000000000000 * %factors{$_};
%factors{"Z$_"} = 1000000000000000000000 * %factors{$_};
%factors{"E$_"} = 1000000000000000000 * %factors{$_};
%factors{"P$_"} = 1000000000000000 * %factors{$_};
%factors{"T$_"} = 1000000000000 * %factors{$_};
%factors{"G$_"} = 1000000000 * %factors{$_};
%factors{"M$_"} = 1000000 * %factors{$_};
%factors{"k$_"} = 1000 * %factors{$_};
%factors{"m$_"} = 0.001 * %factors{$_};
%factors{"u$_"} = 0.000001 * %factors{$_};
%factors{"n$_"} = 0.000000001 * %factors{$_};
%factors{"p$_"} = 0.000000000001 * %factors{$_};
%factors{"a$_"} = 0.000000000000000001 * %factors{$_};
%factors{"z$_"} = 0.000000000000000000001 * %factors{$_};
%factors{"y$_"} = 0.000000000000000000000001 * %factors{$_};
}
%factors;
};
class Isotope {
has Str:D $.isotope;
has Real:D $.half-life;
method parse(Cool:D $line) {
my $isotope = ($line.substr(11, 5) // '').trim;
my $half-life-str = ($line.substr(61, 7) // '').trim;
my $half-life-unit = ($line.substr(69, 2) // '').trim;
unless $isotope ~~ /^\w+$/
&& $half-life-str ~~ /^\d+(\.\d+)?$/
&& (FACTORS{$half-life-unit}:exists) {
return Nil;
}
my $half-life = $half-life-str * FACTORS{$half-life-unit};
Isotope.new(isotope => $isotope, half-life => $half-life);
}
}
for $*ARGFILES.lines {
my $isotope = Isotope.parse($_);
next unless defined $isotope;
say sprintf("%s\t%.40f", $isotope.isotope, $isotope.half-life);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment