0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Perl 6Advent Calendar 2017

Day 9

Advent of Code day6をPerl 6で

Last updated at Posted at 2017-12-10

こんにちは、9日目の投稿になります。
今日は投稿がなかったので、Advent of Code day6の私の回答を貼っておきます。

use v6;                                                                                                                                                
                                                                                                                                                       
my Int @banks = $*IN.get.split("\t")>>.Int;                                                                                                            
                                                                                                                                                       
my %visited;                                                                                                                                           
                                                                                                                                                       
do until %visited{~@banks}:exists {                                                                                                                    
    %visited{~@banks} = True;                                                                                                                          
                                                                                                                                                       
    my $max = @banks.max; # (#1)                                                                                                                              
    my $target-bank-idx = 0;                                                                                                                           
    my $num-redistribution-blocks = 0;                                                                                                                 
    for @banks.pairs -> (:key($idx),:value($v)){ # (#2)                                                                                                      
        if $v == $max {                                                                                                                                
            $target-bank-idx = $idx;                                                                                                                   
            $num-redistribution-blocks = $v;                                                                                                           
            last;                                                                                                                                      
        }                                                                                                                                              
    }                                                                                                                                                  
    @banks[$target-bank-idx] = 0;                                                                                                                      
    my $pos = ($target-bank-idx + 1) % +@banks;                                                                                                        
    while $num-redistribution-blocks-- > 0 {                                                                                                           
        @banks[$pos]++;                                                                                                                                
        $pos++;                                                                                                                                        
        $pos %= +@banks;                                                                                                                               
    }                                                                                                                                                  
    1                                                                                                                                                  
}.sum.say; 
  • ポイント
    • .max でリストの中の最大値を取り出すことができます。 (#1)
    • for @banks.pairs -> (:key($idx),:value($v))は下記のように型を指定して記述することもできます (#2) (参考: https://perl6advent.wordpress.com/2017/12/05/ ):
      • for @banks.pairs -> Pair $p (:key($idx),:value($v))
      • for @banks.pairs -> Pair $p (Int :key($idx), Int :value($v))

以上、9日目の投稿でした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?