Advent of Code 2020: Python Solution Day 5

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 is the 5th day of Advent of Code 2020 and this is more interesting than previous one. But I quickly was able to find the solution. I usually solve solution on next day because I am having hard time managing time. And it is nearly hour left for day 6.
As usual, I have saved text file as day5.txt on same directory as my notebook is and my solution notebook is available at this repository.

with open("day5.txt", "r") as fp:
    lines = fp.readlines()

lines = [line[:-1] for line in lines]

test = ["BFFFBBFRRR", "FFFBBBFRRR", "BBFFBBFRLL"]

# find row, # 0 to 127 rows # f means lower half (0-63) # b means upper half (63-127) # r upper half of column (4-7) # l lower half of column (0-3) 
seat_ids = []
for line in lines:
    #print(f"Current Text: {line}")     # to get row, take first 7 chars     r = line[:7]
    start = 0
    end = 127
    row, col = 0, 0
    for char in r:
        #print(f"Char: {char}")         if char == "F":
            end = int((start+end+1)/2) - 1
        elif char == "B":
            start = int((start+end+1)/2)
        #print(f"Start: {start} End: {end}")     #print("\n")     row = start

    # to get col, take last 3 chars     r = line[7:]
    start = 0
    end = 7
    for char in r:
        #print(f"Char: {char}")         if char == "L":
            end = int((start+end+1)/2) - 1
        elif char == "R":
            start = int((start+end+1)/2)
        #print(f"Start: {start} End: {end}")     col = start
    # seat ID: multiply the row by 8, then add the column     sid = row*8 + col
    seat_ids.append(sid)
    print(f"Row: {row} Column: {col} Seat ID:{sid}")
    print("\n")

print(f"Solution 1: {max(seat_ids)}")
print(f"Solution 2: {[seat for seat in range(min(seat_ids), max(seat_ids)) if seat not in seat_ids][0]}")

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 5

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

请登录后发表评论

    暂无评论内容