Last active
August 29, 2015 14:19
-
-
Save hiratara/4d9b79e0e41faf4237a9 to your computer and use it in GitHub Desktop.
An answer of http://nabetani.sakura.ne.jp/hena/ord30taxi/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Exporter qw(import); | |
our @EXPORT_OK = qw(solve); | |
my @fee_system = ( | |
[995, 400, 60], | |
[845, 350, 50], | |
); | |
my %route = ( | |
AB => [1090, 0], | |
BC => [960, 0], | |
AC => [180, 0], | |
CF => [200, 0], | |
BG => [1270, 0], | |
AD => [540, 1], | |
CD => [400, 1], | |
DF => [510, 1], | |
DE => [720, 1], | |
FG => [230, 1], | |
EG => [1050, 1], | |
); | |
my %city = ( | |
A => 0, | |
B => 0, | |
C => 0, | |
D => 1, | |
E => 1, | |
F => 1, | |
G => 1, | |
); | |
sub _route ($$) { | |
my $key = join '', sort @_; | |
[@{$route{$key}}] or die "NO KEY $key"; | |
} | |
sub solve ($) { | |
my @order = split //, $_[0]; | |
my ($first_d, $first_f, undef) = @{$fee_system[$city{$order[0]}]}; | |
my @routes = map { | |
[$order[$_], $order[$_ + 1], _route($order[$_], $order[$_ + 1])]; | |
} 0 .. $#order - 1; | |
my $total_fee = 0; | |
while (my $route = shift @routes) { | |
my ($start, $end, $r) = @$route; | |
my ($distance, $fee) = @$r; | |
if ($first_d > $distance) { | |
$first_d -= $distance; | |
} elsif ($first_d == $distance) { | |
warn "TODO 1"; | |
return 0; | |
last; | |
} else { | |
$r->[0] -= $first_d; | |
unshift @routes, $route; | |
$total_fee += $first_f; | |
last; | |
} | |
} | |
if ($total_fee == 0) { | |
return $first_f; | |
} | |
my $route; | |
my $left_over = 0; | |
while ($route = shift @routes) { | |
my ($start, $end, $r) = @$route; | |
my ($distance, $f) = @$r; | |
my $fee = $fee_system[$f][2]; | |
# left_over | |
if ($left_over > 0) { | |
$distance -= 200; | |
$distance += $left_over; | |
$left_over = 0; | |
} | |
my $n = int($distance / 200); | |
my $left = $distance % 200; | |
$total_fee += $fee * $n; | |
if ($left == 0) { | |
warn "TODO 3"; | |
} else { | |
$total_fee += $fee if $distance >= 0; | |
$left_over = $left; | |
} | |
} | |
return $total_fee; | |
} | |
1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Solve qw(solve); | |
use Test::More; | |
while (<DATA>) { | |
tr/\r\n//d; | |
my ($no, $input, $output) = split /\t/, $_, 3; | |
is solve $input, $output, "Test $no"; | |
} | |
done_testing; | |
__DATA__ | |
0 ADFC 510 | |
1 CFDA 500 | |
2 AB 460 | |
3 BA 460 | |
4 CD 400 | |
5 DC 350 | |
6 BG 520 | |
7 GB 530 | |
8 FDA 450 | |
9 ADF 450 | |
10 FDACB 750 | |
11 BCADF 710 | |
12 EDACB 800 | |
13 BCADE 810 | |
14 EGFCADE 920 | |
15 EDACFGE 910 | |
16 ABCDA 960 | |
17 ADCBA 1000 | |
18 BADCFGB 1180 | |
19 BGFCDAB 1180 | |
20 CDFC 460 | |
21 CFDC 450 | |
22 ABGEDA 1420 | |
23 ADEGBA 1470 | |
24 CFGB 640 | |
25 BGFC 630 | |
26 ABGEDFC 1480 | |
27 CFDEGBA 1520 | |
28 CDFGEDABG 1770 | |
29 GBADEGFDC 1680 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment