mirror of
https://github.com/jneug/adventofcode.git
synced 2026-04-14 06:43:33 +02:00
Day 4
This commit is contained in:
31
2020/day4_1.py
Normal file
31
2020/day4_1.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
input = []
|
||||||
|
with open('day4_input.txt', 'r') as f:
|
||||||
|
passport = []
|
||||||
|
for line in f.readlines():
|
||||||
|
if len(line.strip()) == 0:
|
||||||
|
input.append(passport)
|
||||||
|
passport = []
|
||||||
|
else:
|
||||||
|
passport += line.strip().split()
|
||||||
|
input.append(passport)
|
||||||
|
|
||||||
|
|
||||||
|
passports = list()
|
||||||
|
p = dict()
|
||||||
|
for passport in input:
|
||||||
|
for kv in passport:
|
||||||
|
k,v = kv.split(':')
|
||||||
|
if k != 'cid':
|
||||||
|
p[k] = v
|
||||||
|
passports.append(p)
|
||||||
|
p = dict()
|
||||||
|
|
||||||
|
|
||||||
|
valid = 0
|
||||||
|
keys = {'byr','iyr','eyr','hgt','hcl','ecl','pid'}
|
||||||
|
for passport in passports:
|
||||||
|
if passport.keys() == keys:
|
||||||
|
valid += 1
|
||||||
|
print(valid)
|
||||||
|
print(passports[-1])
|
||||||
|
print(passports[-2])
|
||||||
53
2020/day4_2.py
Normal file
53
2020/day4_2.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
input = []
|
||||||
|
with open('day4_input.txt', 'r') as f:
|
||||||
|
passport = []
|
||||||
|
for line in f.readlines():
|
||||||
|
if len(line.strip()) == 0:
|
||||||
|
input.append(passport)
|
||||||
|
passport = []
|
||||||
|
else:
|
||||||
|
passport += line.strip().split()
|
||||||
|
input.append(passport)
|
||||||
|
|
||||||
|
|
||||||
|
passports = list()
|
||||||
|
p = dict()
|
||||||
|
for passport in input:
|
||||||
|
for kv in passport:
|
||||||
|
k,v = kv.split(':')
|
||||||
|
if k != 'cid':
|
||||||
|
p[k] = v
|
||||||
|
passports.append(p)
|
||||||
|
p = dict()
|
||||||
|
|
||||||
|
|
||||||
|
def test_hgt(x):
|
||||||
|
if x[-2:] == 'cm':
|
||||||
|
hgt = int(x[:-2])
|
||||||
|
return hgt >= 150 and hgt <= 193
|
||||||
|
elif x[-2:] == 'in':
|
||||||
|
hgt = int(x[:-2])
|
||||||
|
return hgt >= 59 and hgt <= 76
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
valid = 0
|
||||||
|
keys = {
|
||||||
|
'byr': lambda x: re.fullmatch(r'\d{4}', x) and int(x) >= 1920 and int(x) <= 2002,
|
||||||
|
'iyr': lambda x: re.fullmatch(r'\d{4}', x) and int(x) >= 2010 and int(x) <= 2020,
|
||||||
|
'eyr': lambda x: re.fullmatch(r'\d{4}', x) and int(x) >= 2010 and int(x) <= 2030,
|
||||||
|
'hgt': test_hgt,
|
||||||
|
'hcl': lambda x: re.fullmatch(r'#[0-9a-f]{6}', x),
|
||||||
|
'ecl': lambda x: re.fullmatch(r'(amb|blu|brn|gry|grn|hzl|oth)', x),
|
||||||
|
'pid': lambda x: re.fullmatch(r'\d{9}', x)
|
||||||
|
}
|
||||||
|
for passport in passports:
|
||||||
|
if passport.keys() == keys.keys():
|
||||||
|
val = True
|
||||||
|
for k,v in keys.items():
|
||||||
|
val = val and v(passport[k])
|
||||||
|
if val:
|
||||||
|
valid += 1
|
||||||
|
print(valid)
|
||||||
1169
2020/day4_input.txt
Normal file
1169
2020/day4_input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user