Advent of Code 2020: Python Solution Day 4

Advent of Code 2020: Python Solution (25 Part Series)

1 Advent of Code 2020: Python Solution Day 1
2 Advent of Code 2020: Python Solution Day 2
21 more parts…
3 Advent of Code 2020: Python Solution Day 3
4 Advent of Code 2020: Python Solution Day 4
5 Advent of Code 2020: Python Solution Day 5
6 Advent of Code 2020: Python Solution Day 6
7 Advent of Code 2020: Python Solution Day 7
8 Advent of Code 2020: Python Solution Day 8
9 Advent of Code 2020: Python Solution Day 9
10 Advent of Code 2020: Python Solution Day 10
11 Advent of Code 2020: Python Solution Day 11
12 Advent of Code 2020: Python Solution Day 12
13 Advent of Code 2020: Python Solution Day 13
14 Advent of Code 2020: Python Solution Day 14
15 Advent of Code 2020: Python Solution Day 15
16 Advent of Code 2020: Python Solution Day 16
17 Advent of Code 2020: Python Solution Day 17
18 Advent of Code 2020: Python Solution Day 18
19 Advent of Code 2020: Python Solution Day 19
20 Advent of Code 2020: Python Solution Day 20
21 Advent of Code 2020: Python Solution Day 21
22 Advent of Code 2020: Python Solution Day 22
23 Advent of Code 2020: Python Solution Day 23
24 Advent of Code 2020: Python Solution Day 24
25 Advent of Code 2020: Python Solution Day 25

This challenge is more harder than previous ones. Many people are using Regex but I am not comfortable with it so I am going after traditional way. Again the input is saved as day4.txt on same directory as the Jupyter Notebook is. My entire codes is on this repository.
Solution of Challenge 1

with open("day4.txt") as fp:
    lines = [f.strip() for f in fp]

#1 curr_passport = {}
all_passports = []
valid_passports = []
all_keys = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid", "cid"]
key_count = 0
for line in lines:
    if line != "":
        temp_line = line.split(" ")
        temp_dict = {item.split(":")[0]:item.split(":")[1] for item in temp_line}

        for key, value in temp_dict.items():
            curr_passport[key] = value

    else:
        if all(field in curr_passport for field in req_keys):
            valid_passports.append(curr_passport)
        curr_passport = {}
        all_passports.append(curr_passport)
all_passports.append(curr_passport)

len(all_passports), len(valid_passports)        

Enter fullscreen mode Exit fullscreen mode

Solution of Challenge 2

import re
ecl = "amb blu brn gry grn hzl oth".split(" ")
new_valid_passports = []
for passport in valid_passports:
    valid_keys = 0
    for key, value in passport.items():
        #print(passport)         if key == "byr":
            if 1920<=int(value)<=2002:
                valid_keys+=1
        elif key == "iyr":
            if 2010<=int(value)<=2020:
                valid_keys+=1
        elif key == "eyr":
            if 2020<=int(value)<=2030:
                valid_keys+=1
        elif key == "hgt":
            if value[-2:] == "cm" and 150<=int(value[:-2])<=193:
                valid_keys+=1
            elif value[-2:] == "in" and 59<=int(value[:-2])<=76:
                valid_keys+=1
        elif key == "hcl":
            if re.match('^#[0-9a-f]{6}$', value):
                valid_keys+=1
        elif key == "ecl" and value in ecl:
            valid_keys+=1
        elif key == "pid":
            if re.match('^[0-9]{9}$', value):
                valid_keys+=1
    if valid_keys==len(all_keys)-1:
        new_valid_passports.append(passport)
    valid_keys=0

len(new_valid_passports)

Enter fullscreen mode Exit fullscreen mode

I write blogs about Computer Vision projects on my GitHub page q-viper.github.io and if you got some time please share yours too.

Advent of Code 2020: Python Solution (25 Part Series)

1 Advent of Code 2020: Python Solution Day 1
2 Advent of Code 2020: Python Solution Day 2
21 more parts…
3 Advent of Code 2020: Python Solution Day 3
4 Advent of Code 2020: Python Solution Day 4
5 Advent of Code 2020: Python Solution Day 5
6 Advent of Code 2020: Python Solution Day 6
7 Advent of Code 2020: Python Solution Day 7
8 Advent of Code 2020: Python Solution Day 8
9 Advent of Code 2020: Python Solution Day 9
10 Advent of Code 2020: Python Solution Day 10
11 Advent of Code 2020: Python Solution Day 11
12 Advent of Code 2020: Python Solution Day 12
13 Advent of Code 2020: Python Solution Day 13
14 Advent of Code 2020: Python Solution Day 14
15 Advent of Code 2020: Python Solution Day 15
16 Advent of Code 2020: Python Solution Day 16
17 Advent of Code 2020: Python Solution Day 17
18 Advent of Code 2020: Python Solution Day 18
19 Advent of Code 2020: Python Solution Day 19
20 Advent of Code 2020: Python Solution Day 20
21 Advent of Code 2020: Python Solution Day 21
22 Advent of Code 2020: Python Solution Day 22
23 Advent of Code 2020: Python Solution Day 23
24 Advent of Code 2020: Python Solution Day 24
25 Advent of Code 2020: Python Solution Day 25

原文链接:Advent of Code 2020: Python Solution Day 4

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容