Advent of Code 2020: Day 05 with Python

Advent of Code 2020 (26 Part Series)

1 Advent of Code 2020: Day 01 using numpy and vectorized calculations
2 Advent of Code 2020: Day 02(a) using finite state machines
22 more parts…
3 Advent of Code 2020: Day 02(b) using regex named groups
4 Advent of Code 2020: Day 03 using numpy and vectorized calculations
5 Advent of Code 2020: Day 04 using PEG grammars in Python
6 Advent of Code 2020: Day 05 with Python
7 Advent of Code 2020: Day 06 using Python sets
8 Advent of Code 2020: Day 07 using Python PEG grammars + NetworkX
9 Advent of Code 2020: Day 08 using a whole computing cluster to do one CPU’s job
10 Advent of Code 2020: Day 09 more vectorized brute-forcing with numpy in Python
11 Advent of Code 2020: Day 10 using NetworkX, numpy, in Python
12 Advent of Code 2020: Day 11 using TensorFlow in Python
13 Advent of Code 2020: Day 12 using QGIS and Python
14 Advent of Code 2020: Day 13 using…a lot of math, and Numpy in Python
15 Advent of Code 2020: Day 14 using bitwise logic in Python
16 Advent of Code 2020: Day 15 in Python
17 Advent of Code 2020: Day 16 using csv and numpy in Python
18 Advent of Code 2020: Day 17 using 3D/4D Convolution in TensorFlow in Python
19 Advent of Code 2020: Day 18 finally using PEG grammar in Python in the way it’s supposed to
20 Advent of Code 2020: Day 19 abusing PEG grammar in Python the way it’s not supposed to
21 Advent of Code 2020: Day 20 with NetworkX, SciPy cross-correlation in Python
22 Advent of Code 2020: Day 21 with Python sets
23 Advent of Code 2020: Day 22 with Python
24 Advent of Code 2020: Day 23 with linked lists in Python
25 Advent of Code 2020: Day 24 with complex numbers in Python and TensorFlow
26 Advent of Code 2020: Day 25 with Generators in Python

I’m going to keep this one short as I’ve run out of ways to make this interesting. Posting this for completeness.

The Challenge Part 1

Link to challenge on Advent of Code 2020 website

The challenge talks about a weird seating index scheme that uses binary partitioning. However, fancy words aside, they are literally talking about using binary.

One of the examples given was:

BFFFBBFRRR: row 70, column 7, seat ID 567

Enter fullscreen mode Exit fullscreen mode

ignoring the superfluous information about rows and columns (which don’t appear anywhere in the challenge), we just need to know how to convert BFFFBBFRRR into the number 567.

The only solution, really.

Basically it’s binary. if you replace all the B and R with 1, and the F and L with 0, then BFFFBBFRRR turns into 1000110111 which is 567 in binary.

We can do the converting into binary using some simple replace() functions, and then we can turn the ascii string into binary using python’s existing int(value, 2) function. The second argument being the base system.

So, quite simply:

int(entry.replace("B", "1").replace("F", "0").replace("R", "1").replace("L", "0"), 2)

Enter fullscreen mode Exit fullscreen mode

Will give you the seat number from any given string. We can quickly scan the input data using this and list comprehension:

seats = [int(entry.replace("B", "1").replace("F", "0").replace("R", "1").replace("L", "0"), 2) for entry in open("input.txt").readlines()]

Enter fullscreen mode Exit fullscreen mode

The first part of the question asks for the highest seat number in the list. We simply have to do max(seats) to find out

print("highest seat", max(seats))

Enter fullscreen mode Exit fullscreen mode

The Challenge Part 2

The second part of the question says that there is a missing seat somewhere in the middle, but to also ignore the missing seats at either end of the range. We can use set comprehension again for this:

print("my seat", set(range(min(seats), max(seats))).difference(seats))

Enter fullscreen mode Exit fullscreen mode

Tadaa!
Onwards!

Advent of Code 2020 (26 Part Series)

1 Advent of Code 2020: Day 01 using numpy and vectorized calculations
2 Advent of Code 2020: Day 02(a) using finite state machines
22 more parts…
3 Advent of Code 2020: Day 02(b) using regex named groups
4 Advent of Code 2020: Day 03 using numpy and vectorized calculations
5 Advent of Code 2020: Day 04 using PEG grammars in Python
6 Advent of Code 2020: Day 05 with Python
7 Advent of Code 2020: Day 06 using Python sets
8 Advent of Code 2020: Day 07 using Python PEG grammars + NetworkX
9 Advent of Code 2020: Day 08 using a whole computing cluster to do one CPU’s job
10 Advent of Code 2020: Day 09 more vectorized brute-forcing with numpy in Python
11 Advent of Code 2020: Day 10 using NetworkX, numpy, in Python
12 Advent of Code 2020: Day 11 using TensorFlow in Python
13 Advent of Code 2020: Day 12 using QGIS and Python
14 Advent of Code 2020: Day 13 using…a lot of math, and Numpy in Python
15 Advent of Code 2020: Day 14 using bitwise logic in Python
16 Advent of Code 2020: Day 15 in Python
17 Advent of Code 2020: Day 16 using csv and numpy in Python
18 Advent of Code 2020: Day 17 using 3D/4D Convolution in TensorFlow in Python
19 Advent of Code 2020: Day 18 finally using PEG grammar in Python in the way it’s supposed to
20 Advent of Code 2020: Day 19 abusing PEG grammar in Python the way it’s not supposed to
21 Advent of Code 2020: Day 20 with NetworkX, SciPy cross-correlation in Python
22 Advent of Code 2020: Day 21 with Python sets
23 Advent of Code 2020: Day 22 with Python
24 Advent of Code 2020: Day 23 with linked lists in Python
25 Advent of Code 2020: Day 24 with complex numbers in Python and TensorFlow
26 Advent of Code 2020: Day 25 with Generators in Python

原文链接:Advent of Code 2020: Day 05 with Python

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

请登录后发表评论

    暂无评论内容