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 15

Advent of Code day9をPerl 6で

Last updated at Posted at 2017-12-16

こんにちは、15日目になります。
昨日は @debility さんのPerl6のAcmeモジュールをサクッと作った話でした。

独自演算子と言えば、私の知っているモジュールだとΣみたいな足し合わせの演算子をユーザー定義しているものもあったりします: https://github.com/hipek8/p6-Statistics-LinearRegression/blob/master/lib/Statistics/LinearRegression.pm6#L4

・・・今日は特に投稿がなかったので、例によって私のAdvent of Code day9の回答を貼っておきます。

use v6;                                                            
                                                                   
my Bool $garbage = False;                                          
my Bool $skip = False;                                             
my Int $score = 0;                                                 
my Int $depth = 1;                                                 
                                                                   
for $*IN.get.comb -> $char {                                       
    if $skip {                                                     
        $skip = False;                                             
        next;                                                      
    }                                                              
    given $char {                                                  
        when '!' { $skip = True }                                  
        when '{' { $score += $depth++ unless $garbage }            
        when '}' { $depth-- unless $garbage }                      
        when '>' { $garbage = False if $garbage }                  
        when '<' { $garbage = True unless $garbage }               
    }                                                              
}                                                                  
                                                                   
$score.say;                                                        
  • ポイント
    • Grammar-Actions を使うよりも、単純に左から右へ読んでいった方がこの問題の場合は楽です

以上15日目でした

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?