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 13

Advent of Code day8をPerl 6で

Last updated at Posted at 2017-12-14

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

use v6;                                                                     
use MONKEY-SEE-NO-EVAL; # (#1)                                                    
                                                                            
grammar Grammar {                                                           
    rule TOP {                                                              
        <register-name> <command> <value> <condition>                       
    }                                                                       
    token register-name {                                                   
        \w+                                                                 
    }                                                                       
    proto token command {*}                                                 
    token command:sym<inc> {                                                
        <sym>                                                               
    }                                                                       
    token command:sym<dec> {                                                
        <sym>                                                               
    }                                                                       
    token value {                                                           
        \-? \d+                                                             
    }                                                                       
    rule condition {                                                        
        'if' <register-name> <comparator> <value>                           
    }                                                                       
    proto token comparator {*}                                              
    token comparator:sym<eq> {                                              
        '=='                                                                
    }                                                                       
    token comparator:sym<ne> {                                              
        '!='                                                                
    }                                                                       
    token comparator:sym<lt> {                                              
        '<'                                                                 
    }                                                                       
    token comparator:sym<gt> {                                              
        '>'                                                                 
    }                                                                       
    token comparator:sym<gteq> {                                            
        '>='                                                                
    }                                                                       
    token comparator:sym<lteq> {                                            
        '<='                                                                
    }                                                                       
}                                                                           
                                                                            
my %register;  
                                                        
class Actions {                                                             
    method TOP($/) {                                                        
        return unless $<condition>.made;                                    
        given ~$<command> {                                                 
            when 'inc' {                                                    
                %register{~$<register-name>} += $<value>;                   
            }                                                               
            when 'dec' {                                                    
                %register{~$<register-name>} -= $<value>;                   
            }                                                               
        }                                                                   
    }                                                                       
    method condition($/) {                                                  
        my $name = $<register-name>;                                        
        %register{~$name} = 0 unless %register{~$name}:exists;              
        make EVAL("so " ~ ('%register<'~$name~'>',                          
                           ~$<comparator>,                                  
                           +$<value>).join(" "));                           
    }                                                                       
}                                                                           
                                                                            
Grammar.parse($_, actions => Actions) for $*IN.lines;                       
%register.sort(*.value).tail.say;                                                    
  • ポイント
    • Grammarをつかうときれいにかけます
    • MONKEY-SEE-NO-EVALをuseするとEVALが使えるようになります (#1)
      • ※ 半分遊びでやってるだけで、当然こんな機能非推奨です

以上、13日目でした。

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?