Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use List::Util qw(max);
- my @roll3d3 = (1, 3, 6, 7, 6, 3, 1);
- my @Play; # an array (player) of 2D arrays of positions x scores => num games
- while (<>) {
- my ($play, $pos) = m#Player (\d+) starting position: (\d+)#;
- $Play[$play-1][$pos][0] = 1;
- }
- my @Active = (1,1); # number of active games for player
- my @Wins = (0,0); # number of winning universes accumulated
- my $p = 1; # current active player
- while (1) {
- my @next; # 2D array of positions x scores => num games
- my $act = 0; # count of total active games for this player this turn
- $p = !$p; # change active player
- foreach my $pos (1 .. 10) {
- foreach my $score (0 .. 20) {
- next if (!$Play[$p][$pos][$score]);
- # spread 3d3 roll across the multiverse
- my $i = ($pos + 2) % 10 + 1;
- foreach my $games (@roll3d3) {
- if ($score + $i >= 21) {
- $Wins[$p] += $Play[$p][$pos][$score] * $games * $Active[!$p];
- } else {
- $next[$i][$score + $i] += $Play[$p][$pos][$score] * $games;
- $act += $Play[$p][$pos][$score] * $games;
- }
- $i = $i % 10 + 1;
- }
- }
- }
- $Play[$p] = \@next;
- $Active[$p] = $act;
- last if ($act == 0);
- }
- print "Part 2: ", max(@Wins), "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement