]> git.sommitrealweird.co.uk Git - advent-of-code-2020.git/blob - day5/get_seat_id.py
Add a python version
[advent-of-code-2020.git] / day5 / get_seat_id.py
1 #!/usr/bin/python3
2
3 def get_row(binary_id):
4     choices=binary_id[:7]
5
6     selections=[a for a in range(128)]
7
8     for decision in choices:
9         half_range=int(len(selections)/2)
10         if decision == 'F':
11             del selections[-half_range:]
12         elif decision == 'B':
13             del selections[:half_range]
14
15     return selections[0]
16
17 def get_col(binary_id):
18     choices=binary_id[-3:]
19
20     selections=[a for a in range(8)]
21
22     for decision in choices:
23         half_range=int(len(selections)/2)
24         if decision == 'L':
25             del selections[-half_range:]
26         elif decision == 'R':
27             del selections[:half_range]
28
29     return selections[0]
30
31 def get_seat(binary_id):
32     row=get_row(binary_id)
33     col=get_col(binary_id)
34
35     return ((row,col))
36
37 def get_seat_id(row, col):
38     return ((row * 8) + col)
39
40 all_seat_ids=[]
41
42 for line in open("input.txt", "r"):
43     line = line.rstrip()
44
45     (row, col) = get_seat(line)
46     all_seat_ids.append(get_seat_id(row, col))
47
48 # sort the list
49 all_seat_ids.sort()
50
51 # and print the highest seat id - part 1
52 print("Highest seat id: {}".format(all_seat_ids[-1]))
53
54
55 # part 2
56 my_seat_id=None
57
58 for a,b in enumerate(all_seat_ids, start=all_seat_ids[0]):
59     if a != b:
60         my_seat_id=a
61         break
62
63 print("My seat id: {}".format(my_seat_id))