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.

Advent of Code day7をPerl 6で

Last updated at Posted at 2017-12-14

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

use v6;                                                                       
                                                                              
grammar Grammar {                                                             
    token TOP {                                                               
        [                                                                     
            <name> <.ws> '('<weight>')' [ <.ws> '->' <.ws> <namelist> ]?      
        ]                                                                     
    }                                                                         
    token namelist {                                                          
        <name>+ % ', '                                                        
    }                                                                         
    token name {                                                              
        \w+                                                                   
    }                                                                         
    token weight {                                                            
        \d+                                                                   
    }                                                                         
}                                                                             
                                                                              
                                                                              
my %is-root;                                                                  
for $*IN.lines -> $line {                                                     
    my $m = Grammar.parse($line);                                             
    %is-root{~$m<name>} = True unless %is-root{~$m<name>}:exists;             
                                                                              
    if $m<namelist>:exists {                                                  
        for $m<namelist><name> {                                              
            %is-root{~$_} = False;                                            
        }                                                                     
    }                                                                         
}                                                                             
                                                                              
do for %is-root -> (:key($name), :value($)) {                                 
    $name if %is-root{$name};                                                 
}.head.say;                                                                   
  
  • ポイント
    • Grammar使うときれいにかけます。
    • .oO( weightとは何だったんでしょうか??? )

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

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?