Advent of Code 2020: Python Solution Day 2

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 can be found at this link.

My solution is:

with open("day2.txt", "r") as f:
    inp=f.readlines()

linp = [i.split("\n")[0] for i in inp]
# print(linp) valid_2 = []
valid_1 = []
for lin in linp:
    key, value = lin.split(": ")
    char = key.split(" ")[1]
    # print(char, key.split(" ")[0].split("-"))     n1, n2 = key.split(" ")[0].split("-")
    n1, n2 = int(n1)-1, int(n2)-1

    if n1+1<=value.count(char)<=n2+1:
        valid_1.append(lin)

    c = 0
    try:

        if value[int(n1)] == char:
            c+=1
        if value[int(n2)] == char:
            c+=1
        if c == 1:
            valid_2.append(lin)
    except:
        pass
print(f" Solution 1: {len(valid_1)}\nSolution 2:{len(valid_2)}")

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 2

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

请登录后发表评论

    暂无评论内容