Advertisement
ralig

Advent Of Code 2020 Day 4 Part 1

Dec 4th, 2020
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. from pathlib import Path
  2.  
  3. class Passenger:
  4.     def __init__(self, byr,iyr,eyr,hgt,hcl,ecl,pid,cid):
  5.         self.byr = byr
  6.         self.iyr = iyr
  7.         self.eyr = eyr
  8.         self.hgt = hgt
  9.         self.hcl = hcl
  10.         self.ecl = ecl
  11.         self.pid = pid
  12.         self.cid = cid
  13.  
  14. def parseRecord(record):
  15.     person = Passenger("","","","","","","","")
  16.     info = record.split()
  17.     for item in info:
  18.         recs = item.split(":")
  19.         setattr(person,recs[0],recs[1])
  20.     return(person)
  21.  
  22. def determineIfValid(person):
  23.     for attr, value in person.__dict__.items():
  24.         if (value != ""):
  25.             pass
  26.         else:
  27.             if (attr != "cid"):
  28.                 return False
  29.     return True
  30.  
  31. path = Path(__file__).parent / "../../input.txt"
  32.  
  33. countValids = 0
  34. with path.open("rt") as f:
  35.     line = True
  36.     while (line):
  37.         foundEntireRecord = False
  38.         rec = ""
  39.         while not foundEntireRecord:
  40.             line = f.readline()
  41.             if ((line != "\n") and (line != '')):
  42.                 rec += line
  43.             else:
  44.                 foundEntireRecord = True
  45.                 person = parseRecord(rec)
  46.                 if(determineIfValid(person)):
  47.                     countValids += 1
  48.  
  49. print(countValids)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement