|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +importos,grid,graph,dot,assembly,re,itertools |
| 3 | +fromcollectionsimportCounter,deque,defaultdict |
| 4 | + |
| 5 | +fromcompassimport* |
| 6 | + |
| 7 | + |
| 8 | +# This functions come from https://github.com/mcpower/adventofcode - Thanks! |
| 9 | +deflmap(func,*iterables): |
| 10 | +returnlist(map(func,*iterables)) |
| 11 | + |
| 12 | + |
| 13 | +defints(s:str): |
| 14 | +returnlmap(int,re.findall(r"-?\d+",s))# thanks mserrano! |
| 15 | + |
| 16 | + |
| 17 | +defpositive_ints(s:str): |
| 18 | +returnlmap(int,re.findall(r"\d+",s))# thanks mserrano! |
| 19 | + |
| 20 | + |
| 21 | +deffloats(s:str): |
| 22 | +returnlmap(float,re.findall(r"-?\d+(?:\.\d+)?",s)) |
| 23 | + |
| 24 | + |
| 25 | +defpositive_floats(s:str): |
| 26 | +returnlmap(float,re.findall(r"\d+(?:\.\d+)?",s)) |
| 27 | + |
| 28 | + |
| 29 | +defwords(s:str): |
| 30 | +returnre.findall(r"[a-zA-Z]+",s) |
| 31 | + |
| 32 | + |
| 33 | +test_data= {} |
| 34 | + |
| 35 | +test=1 |
| 36 | +test_data[test]= { |
| 37 | +"input":"""ecl:gry pid:860033327 eyr:2020 hcl:#fffffd |
| 38 | +byr:1937 iyr:2017 cid:147 hgt:183cm |
| 39 | +
|
| 40 | +iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884 |
| 41 | +hcl:#cfa07d byr:1929 |
| 42 | +
|
| 43 | +hcl:#ae17e1 iyr:2013 |
| 44 | +eyr:2024 |
| 45 | +ecl:brn pid:760753108 byr:1931 |
| 46 | +hgt:179cm |
| 47 | +
|
| 48 | +hcl:#cfa07d eyr:2025 pid:166559648 |
| 49 | +iyr:2011 ecl:brn hgt:59in""", |
| 50 | +"expected": ["2","Unknown"], |
| 51 | +} |
| 52 | +test=2 |
| 53 | +test_data[test]= { |
| 54 | +"input":"""eyr:1972 cid:100 |
| 55 | +hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926 |
| 56 | +
|
| 57 | +iyr:2019 |
| 58 | +hcl:#602927 eyr:1967 hgt:170cm |
| 59 | +ecl:grn pid:012533040 byr:1946 |
| 60 | +
|
| 61 | +hcl:dab227 iyr:2012 |
| 62 | +ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277 |
| 63 | +
|
| 64 | +hgt:59cm ecl:zzz |
| 65 | +eyr:2038 hcl:74454a iyr:2023 |
| 66 | +pid:3556412378 byr:2007 |
| 67 | +
|
| 68 | +pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980 |
| 69 | +hcl:#623a2f |
| 70 | +
|
| 71 | +eyr:2029 ecl:blu cid:129 byr:1989 |
| 72 | +iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm |
| 73 | +
|
| 74 | +hcl:#888785 |
| 75 | +hgt:164cm byr:2001 iyr:2015 cid:88 |
| 76 | +pid:545766238 ecl:hzl |
| 77 | +eyr:2022 |
| 78 | +
|
| 79 | +iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719""", |
| 80 | +"expected": ["Unknown","4"], |
| 81 | +} |
| 82 | + |
| 83 | +test="real" |
| 84 | +input_file=os.path.join( |
| 85 | +os.path.dirname(__file__), |
| 86 | +"Inputs", |
| 87 | +os.path.basename(__file__).replace(".py",".txt"), |
| 88 | +) |
| 89 | +test_data[test]= { |
| 90 | +"input":open(input_file,"r+").read(), |
| 91 | +"expected": ["235","194"], |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +# -------------------------------- Control program execution ------------------------- # |
| 96 | + |
| 97 | +case_to_test="real" |
| 98 | +part_to_test=2 |
| 99 | + |
| 100 | +# -------------------------------- Initialize some variables ------------------------- # |
| 101 | + |
| 102 | +puzzle_input=test_data[case_to_test]["input"] |
| 103 | +puzzle_expected_result=test_data[case_to_test]["expected"][part_to_test-1] |
| 104 | +puzzle_actual_result="Unknown" |
| 105 | + |
| 106 | + |
| 107 | +# -------------------------------- Actual code execution ----------------------------- # |
| 108 | + |
| 109 | +required_fields= ["byr","iyr","eyr","hgt","hcl","ecl","pid"] |
| 110 | + |
| 111 | +passports= [] |
| 112 | +i=0 |
| 113 | +forstringinpuzzle_input.split("\n"): |
| 114 | +iflen(passports)>=i: |
| 115 | +passports.append("") |
| 116 | +ifstring=="": |
| 117 | +i=i+1 |
| 118 | +else: |
| 119 | +passports[i]=passports[i]+" "+string |
| 120 | + |
| 121 | +valid_passports=0 |
| 122 | + |
| 123 | +ifpart_to_test==1: |
| 124 | +forpassportinpassports: |
| 125 | +ifall([x+":"inpassportforxinrequired_fields]): |
| 126 | +valid_passports=valid_passports+1 |
| 127 | + |
| 128 | + |
| 129 | +else: |
| 130 | +forpassportinpassports: |
| 131 | +ifall([x+":"inpassportforxinrequired_fields]): |
| 132 | +fields=passport.split(" ") |
| 133 | +score=0 |
| 134 | +forfieldinfields: |
| 135 | +data=field.split(":") |
| 136 | +ifdata[0]=="byr": |
| 137 | +year=int(data[1]) |
| 138 | +ifyear>=1920andyear<=2002: |
| 139 | +score=score+1 |
| 140 | +elifdata[0]=="iyr": |
| 141 | +year=int(data[1]) |
| 142 | +ifyear>=2010andyear<=2020: |
| 143 | +score=score+1 |
| 144 | +elifdata[0]=="eyr": |
| 145 | +year=int(data[1]) |
| 146 | +ifyear>=2020andyear<=2030: |
| 147 | +score=score+1 |
| 148 | +elifdata[0]=="hgt": |
| 149 | +size=ints(data[1])[0] |
| 150 | +ifdata[1][-2:]=="cm": |
| 151 | +ifsize>=150andsize<=193: |
| 152 | +score=score+1 |
| 153 | +elifdata[1][-2:]=="in": |
| 154 | +ifsize>=59andsize<=76: |
| 155 | +score=score+1 |
| 156 | +elifdata[0]=="hcl": |
| 157 | +ifre.match("#[0-9a-f]{6}",data[1])andlen(data[1])==7: |
| 158 | +score=score+1 |
| 159 | +print(data[0],passport) |
| 160 | +elifdata[0]=="ecl": |
| 161 | +ifdata[1]in ["amb","blu","brn","gry","grn","hzl","oth"]: |
| 162 | +score=score+1 |
| 163 | +print(data[0],passport) |
| 164 | +elifdata[0]=="pid": |
| 165 | +ifre.match("[0-9]{9}",data[1])andlen(data[1])==9: |
| 166 | +score=score+1 |
| 167 | +print(data[0],passport) |
| 168 | +print(passport,score) |
| 169 | +ifscore==7: |
| 170 | +valid_passports=valid_passports+1 |
| 171 | + |
| 172 | +puzzle_actual_result=valid_passports |
| 173 | + |
| 174 | +# -------------------------------- Outputs / results --------------------------------- # |
| 175 | + |
| 176 | +print("Case :",case_to_test,"- Part",part_to_test) |
| 177 | +print("Expected result : "+str(puzzle_expected_result)) |
| 178 | +print("Actual result : "+str(puzzle_actual_result)) |