パスポート処理
コード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:第1部
つのアルゴリズム成分を考慮している
アルゴリズム
Considering three algorithmic ingredients
正規表現
各々のパスポートのための
/(ecl|pid|eyr|hcl|byr|iyr|cid|hgt)/g
長さ
マッチのリストから
含む
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部
オブジェクト
ユーティリティ関数のリテラルと正規表現
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
私はそれが定期的な式を必要とすることを知っていたので、私がそれを読んだ最初にこのパズルをスキップして思い出す.今回は、それぞれのレゲエを楽しく楽しく感じました.
これらの初期の日のパズルは予想よりも簡単に感じるときにやりがいを感じる続けている.
Reference
この問題について(パスポート処理), 我々は、より多くの情報をここで見つけました https://dev.to/rmion/passport-processing-35j2テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol