#!/usr/bin/perl
use warnings;
use strict;
$\ = $/;
while(<>){
@_ = map ~~<>, 1 .. $r + 3;
my $next_to_exit = 0;
my $distance_to_exit = 0;
my $LR_balance = 0;
for( @_ ){
/^\./ and $distance_to_exit = 0;
$next_to_exit = 1 == $distance_to_exit ? 1 : 0;
my $minuses = () = /-/g;
while( /#/g ){
$LR_balance += 6 <=> pos; }
$_ = {
line => $_,
next_to_exit => $next_to_exit,
distance_to_exit => $distance_to_exit,
minuses => $minuses,
};
$distance_to_exit ++;
}
$_->{ line } =~ /^\./ and $distance_to_exit = 0;
$_->{ distance_to_exit } > $distance_to_exit and
$_->{ distance_to_exit } = $distance_to_exit;
$distance_to_exit ++;
}
for my $lett ( ('a' .. 'z')[ 0 .. $n - 1 ] ){
|| $b->{ next_to_exit } * !! $b->{ minuses } <=> $a->{ next_to_exit } * !! $a->{ minuses }
|| $b->{ minuses } <=> $a->{ minuses }
|| $a->{ distance_to_exit } <=> $b->{ distance_to_exit }
} @_;
for my $pos ( map { 6 + $_ * ( $LR_balance <=> .5
) } map { $_, -$_ } 1, 3, 5, 0, 4 ){ ( substr $ref->{ line
}, $pos - 1, 1 ) =~ s/-/$lett/ and do { $ref->{ minuses } --;
$LR_balance -= $pos <=> 6;
last;
};
}
}
print $_->{ line
} for @_; }
IyEvdXNyL2Jpbi9wZXJsCgp1c2Ugd2FybmluZ3M7CnVzZSBzdHJpY3Q7CgokXCA9ICQvOwoKd2hpbGUoPD4pewoJbXkoICRyLCAkbiApID0gc3BsaXQ7CglAXyA9IG1hcCB+fjw+LCAxIC4uICRyICsgMzsKCWNob21wIEBfOwoJCQoJbXkgJG5leHRfdG9fZXhpdCA9IDA7CglteSAkZGlzdGFuY2VfdG9fZXhpdCA9IDA7CgkKCW15ICRMUl9iYWxhbmNlID0gMDsKCQoJZm9yKCBAXyApewoJCS9eXC4vIGFuZCAkZGlzdGFuY2VfdG9fZXhpdCA9IDA7CgkJCgkJJG5leHRfdG9fZXhpdCA9IDEgPT0g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---
...........
---.---.---
...........
6 26
...........
---.---.###
#-#.---.---
---.###.---
...........
---.###.---
#--.#-#.--#
#--.--#.#-#
...........
8 6
...........
###.###.###
###.###.##-
###.###.##-
###.###.##-
...........
###.###.###
###.###.##-
###.###.##-
###.###.##-
...........
14 12
...........
###.###.###
###.###.##-
###.###.##-
###.###.##-
###.###.##-
###.###.##-
###.###.##-
...........
###.###.###
###.###.##-
###.###.##-
###.###.##-
###.###.##-
###.###.##-
###.###.##-
...........