ソースを参照

2020/4

master
Roderic Day 4年前
コミット
628aa1f9dd
1個のファイルの変更30行の追加0行の削除
  1. +30
    -0
      y2020/p04.py

+ 30
- 0
y2020/p04.py ファイルの表示

@@ -0,0 +1,30 @@
import re
import sys


def checks(byr, iyr, eyr, hgt, hcl, ecl, pid, cid=None):
yield 1920 <= int(byr) <= 2002
yield 2010 <= int(iyr) <= 2020
yield 2020 <= int(eyr) <= 2030
if hgt.endswith('cm'):
yield 150 <= int(hgt[:-2]) <= 193
elif hgt.endswith('in'):
yield 59 <= int(hgt[:-2]) <= 76
else:
yield False
yield re.match(r'^#[0-9a-f]{6}$', hcl)
yield re.match(r'^(amb|blu|brn|gry|grn|hzl|oth)$', ecl)
yield re.match(r'^\d{9}$', pid)


ans1 = 0
ans2 = 0
fields = {'byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid'}
for blob in sys.stdin.read().split('\n\n'):
passport = dict(re.findall(r'(\S+):(\S+)', blob))
valid = not fields.difference(passport)
ans1 += valid
if valid:
ans2 += all(checks(**passport))
print(ans1)
print(ans2)

読み込み中…
キャンセル
保存