Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env perl
use strict;
use warnings;
# usage:
# $ perl decode_nw-7.pl 12121121211112111121121112111121122111111212112
# -> B5468B
my %characters = (
'1111122' => '0',
'1111221' => '1',
'1112112' => '2',
'2211111' => '3',
'1121121' => '4',
'2111121' => '5',
'1211112' => '6',
'1211211' => '7',
'1221111' => '8',
'2112111' => '9',
'1112211' => '-',
'1122111' => '$',
'2111212' => ':',
'2121112' => '/',
'2121211' => '.',
'1121212' => '+',
'1122121' => 'A',
'1212112' => 'B',
'1112122' => 'C',
'1112221' => 'D',
);
my $input = $ARGV[0];
my $buf = "";
my $separator_flag = 0;
foreach (split //, $input) {
if ($separator_flag == 1) {
die "decode error: not found separator" if $_ ne "1";
$separator_flag = 0;
next;
}
$buf .= $_;
if (length($buf) == 7) {
if (!exists($characters{$buf})) {
die "decode error: not exist character";
}
print $characters{$buf};
$buf = "";
$separator_flag = 1;
}
}
if (length($buf) > 0) {
die "decode error: broken code";
}
print "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.