LoginSignup
0
0

More than 5 years have passed since last update.

Advent of Code day3をPerl 6で

Last updated at Posted at 2017-12-06

こんにちは、6日目の投稿になります。

例によって、私のAdvent of Code day3の回答を貼っておきます。

use v6;                                                                                                                                                

sub right(Int \n --> Int) {                                                                                                                            
    4 * n ** 2 - 3 * n + 1                                                                                                                             
}                                                                                                                                                      

sub top(Int \n --> Int) {                                                                                                                              
    4 * n ** 2 - 9 * n + 6                                                                                                                             
}                                                                                                                                                      

sub left(Int \n --> Int) {                                                                                                                             
    4 * n ** 2 - 7 * n + 4                                                                                                                             
}                                                                                                                                                      

sub bottom(Int \n --> Int) {                                                                                                                           
    4 * n ** 2 + 3 * n + 1                                                                                                                             
}                                                                                                                                                      

my $target = 277678;                                                                                                                                   
say $_ + abs($target - right($_)) if right($_) - $_ <= $target <= right($_) + $_ for 2..2000;                                                          
say $_ + abs($target - top($_)) - 1 if top($_) - $_ - 1 <= $target <= top($_) + $_ - 1 for 2..2000;                                                    
say $_ + abs($target - left($_)) - 1 if left($_) - $_ - 1 <= $target <= left($_) + $_ - 1 for 2..2000;                                                 
say $_ + abs($target - bottom($_)) if bottom($_) - $_ <= $target <= bottom($_) + $_ for 2..2000;                                                       
  • ポイント
    • OEISで整数列いれて式調べました
    • \n はsigilless variableです。ふつうは値の代入の際にコンテキストを変更させたくない場合に使います。が、今回はsigilがない方が若干式っぽく見えるので使いました。
    • bottom($_) - $_ <= $target <= bottom($_) + $_ Perl6ではこういう感じで不等号を書くことができます

もうちょっときれいに書けそう・・・

以上6日目の投稿でした。

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