パスポート処理


コード2020日4の出現


タスク:Xのどこに解決する.


X = the number of valid passports
  • すべての必須キーは現在
  • である
  • 必要なキーに関連するすべての値は、正しい
  • です

    例入力


    ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
    byr:1937 iyr:2017 cid:147 hgt:183cm
    
    iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
    hcl:#cfa07d byr:1929
    
    hcl:#ae17e1 iyr:2013
    eyr:2024
    ecl:brn pid:760753108 byr:1931
    hgt:179cm
    
    hcl:#cfa07d eyr:2025 pid:166559648
    iyr:2011 ecl:brn hgt:59in
    
    It represents:
  • パスポートデータ
  • のリスト
  • 各パスポートは、最大8キー:値ペア
  • 含まれています

    第1部


    つのアルゴリズム成分を考慮している

  • アルゴリズム
  • を書く

  • Considering three algorithmic ingredients

  • レギュラー式
  • を含みます

    正規表現


    各々のパスポートのための

  • /(ecl|pid|eyr|hcl|byr|iyr|cid|hgt)/g
    

    長さ


    マッチのリストから
  • 、カウント
  • をチェックしてください
  • が8ならば、パスポートは有効でなければなりません
  • ならば、パスポートは無効でなければなりません

    含む

  • が7ならば、cidをキーとしてチェックしてください.
  • ならば、我々は必要なキーを逃していることを意味しますので、無効にしなければなりません
  • がなければ、我々はすべての必要なキーを持っていなければならないので、パスポートは有効でなければなりません
  • アルゴリズムの記述


    Split the input at each double-new-line character into an array of strings
    
    For each item in the array of strings
      Change the item to an array of matched keys found using the regular expression above
    
    Filter the array of matched keys to only include 'true' values where:
        1. The array's length is 8
        2. The array's length is 7 and it doesn't include 'cid'
    
    Return the length of the filtered array
    

    第2部

  • ユーティリティ関数と正規表現のlitany
    オブジェクト
  • にメソッドとして機能を記憶する
  • ルート正規表現
  • への小さな追加
  • ワーキングアルゴリズム
  • を書く

    ユーティリティ関数のリテラルと正規表現

  • 必要なキーが存在するパスポートを識別することを越えて、私は現在、彼らの価値が有効であると保証しなければなりません
  • 私は、それぞれのキーの値
  • をチェックするためにユーティリティ関数と正規表現を必要とするようですbyr iyr eyrも同様である.
    Return true if:
      String length is 4
      Value is within a range
    
    hgtは正規表現です.
    Regular expression:
    /(\d+)(\w+)/
    
    Return true if:
      String matches the regular expression
      Depending on 'cm' or 'in', the value is within a range
    
    hclは正規表現です:
    /#([0-9a-f]{6})/
    
    eclは正規表現です:
    /(amb|blu|brn|gry|grn|hzl|oth){1}/
    
    pidは正規表現です:
    /^\d{9}$/
    
    cidは常にtrueです.

    オブジェクトをメソッドとして格納する


    rules = {
      byr()...,
      hgt()...,
      ...
    }
    
    各キー:値ペアマッチはこの構造を持っています
    ['byr', '2000']
    
    それで、テストを実行することができます.
    rules[key](value)
    

    ルート正規表現への小さな追加


    第1部では、各キーをキャプチャする正規表現を示しました.
    各々のキーが8つの3 -手紙組合せのうちの1つでありえる

  • /(ecl|pid|eyr|hcl|byr|iyr|cid|hgt)/g
    
    Part 2キーと値をキャプチャする正規表現が必要です.
    値が1つ以上の文字、1つ以上の数字、または単一のハッシュタグである可能性がある

  • /(ecl|pid|eyr|hcl|byr|iyr|cid|hgt):([\w|\d|#]+)/g
    

    アルゴリズムの記述


    Split the input at each double-new-line character into an array of strings
    
    For each item in the array of strings
      Change the item to an array of matched keys found using the updated regular expression above
    
    Filter the array of matched keys to only include 'true' values where:
      1. The array's length is 8
      2. The array's length is 7 and it doesn't include 'cid'
    
    Filter the array of valid - in that all required keys are present - passports to only include 'true' values where:
      Every value for each key returns 'true' for the appropriate test found in the 'rules' object for that key
    
    Return the length of the filtered array
    
    私はそれが定期的な式を必要とすることを知っていたので、私がそれを読んだ最初にこのパズルをスキップして思い出す.
    今回は、それぞれのレゲエを楽しく楽しく感じました.
    これらの初期の日のパズルは予想よりも簡単に感じるときにやりがいを感じる続けている.