summaryrefslogtreecommitdiff
path: root/2020/aoc2020-d05.py
blob: ffc6ffb355e8e9daf5ce80aea1dd07fa6aa39927 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#advent of code 2020
#day 05
#examples = ["BFFFBBFRRR","FFFBBBFRRR","BBFFBBFRLL"];
def partitioner(instr, limLow, limHigh, splitLow, splitHigh):
	diff = limHigh - limLow + 1;
	if instr == "":
		return limLow;
	elif instr[0] == splitLow:
		return partitioner(instr[1:], limLow, limHigh-diff//2, splitLow, splitHigh);
	elif instr[0] == splitHigh:
		return partitioner(instr[1:], limLow+diff//2, limHigh, splitLow, splitHigh);

p1 = 0;
AllSeats = [];
PuzzleInput = open("05.in","r");

for BoardingPass in PuzzleInput:
	BP = BoardingPass.replace("\n","");
	row = partitioner(BP[:-3],0,127,"F","B");
	col = partitioner(BP[-3:],0,7,"L","R");
	seatID = row*8 + col;
	p1 = max(p1,seatID)
	AllSeats.append(seatID)
PuzzleInput.close();

AllSeats = sorted(AllSeats);
for n in range(1, len(AllSeats)):
	if AllSeats[n] - AllSeats[n-1] != 1:
		p2 = AllSeats[n-1] + 1;
		break;

print("part 1 = ",p1);
print("part 2 = ",p2);