initial commit on day 9
This commit is contained in:
commit
f896e7c753
31 changed files with 8328 additions and 0 deletions
23
4/1.py
Normal file
23
4/1.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
def validate(entry, required_fields):
|
||||
for field in required_fields:
|
||||
if field not in entry:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
entries = []
|
||||
entry = {}
|
||||
with open("input") as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if not line:
|
||||
entries.append(entry)
|
||||
entry = {}
|
||||
else:
|
||||
for field in line.split(" "):
|
||||
key, value = field.split(":")
|
||||
entry[key] = value
|
||||
entries.append(entry)
|
||||
|
||||
required = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid']
|
||||
print(list(map(lambda x: validate(x, required), entries)).count(True))
|
||||
54
4/2.py
Normal file
54
4/2.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
def validate_int(value, mi, ma):
|
||||
value = int(value)
|
||||
return mi <= value <= ma
|
||||
|
||||
def validate_height(value):
|
||||
v = int(value[:-2])
|
||||
t = value[-2:]
|
||||
if (t == "cm" and 150<=v<=193) or (t == "in" and 59<=v<=76):
|
||||
return True
|
||||
return False
|
||||
|
||||
def validate_hex(h):
|
||||
return h[0] == "#" and len(h[1:]) == 6 and is_int(h[1:], base=16)
|
||||
|
||||
def is_int(x, base=10):
|
||||
try:
|
||||
int(x, base=base)
|
||||
except ValueError:
|
||||
return False
|
||||
return True
|
||||
|
||||
validators = {
|
||||
'byr': lambda x: validate_int(x, 1920, 2002),
|
||||
'iyr': lambda x: validate_int(x, 2010, 2020),
|
||||
'eyr': lambda x: validate_int(x, 2020, 2030),
|
||||
'hgt': lambda x: validate_height(x),
|
||||
'hcl': lambda x: validate_hex(x),
|
||||
'ecl': lambda x: x in ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'],
|
||||
'pid': lambda x: len(x) == 9 and is_int(x)
|
||||
}
|
||||
|
||||
|
||||
def validate(entry, required_fields):
|
||||
for field in required_fields:
|
||||
if field not in entry or not validators[field](entry[field]):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
entries = [{}]
|
||||
index = 0
|
||||
with open("input") as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if not line:
|
||||
entries.append({})
|
||||
index = index + 1
|
||||
else:
|
||||
for entry in line.split(" "):
|
||||
key, value = entry.split(":")
|
||||
entries[index][key] = value
|
||||
|
||||
required = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid']
|
||||
print(list(map(lambda x: validate(x, required), entries)).count(True))
|
||||
Loading…
Add table
Add a link
Reference in a new issue