Posts

Advent of Code Day 5

Dec 5, 2020 | 4 minutes to read

Tag: geeky

OK, today’s Day 5 Advent of Code puzzles were fun! They have you reviewing a list of boarding passes which were stored in a crazy “binary space partitioning” system. It’s really just a complicated way of obfuscating the seat’s row and column, it turns out, so you just have to write some code for parsing a string like, “FBBBFBFBLLR” and turn that into a seat number on the airplane.

I liked writing this code because I’ve never hgad to think about a dataset like this before and it allowed me to think about things like the difference between counting rows on the plane and counting array entries. I also learned a little about rounding and ‘math.ceil’ in Python, plus NumPy too. I thought I needed NumPy for the second part of this puzzle, but I didn’t end up using it.

Here’s my code for 2020 Day 5 part 1:

# Advent of Code 2020 Day 5 - What is the highest seat ID on a boarding pass?
import math
log = ''
passcount = 0
highestSeatID = 0
lineCtr = 1

def addToLog(value):
    global log
    log += value + "\n"

with open("c:\\git\\advent-2020\\5.txt", "r") as f:
    seatlist = f.read().splitlines() 
f.close()

for line in seatlist:
    rowHigh = 127
    rowLow = 0
    colHigh = 7
    colLow = 0
    addToLog("line " + str(lineCtr) + "    " + line)
    if line == "":
        break
    for i in range(0,10):
        # example : BFBBFFFLRR
        lettr = line[i]
        if lettr == "F":
            if i == 7:
                rowHigh = rowHigh - math.ceil(rowHigh - rowLow) / 2
            else:
                rowHigh = rowHigh - math.ceil((rowHigh - rowLow) / 2)
        elif lettr == "B":
            if i == 7:
                rowLow = rowLow + math.ceil((rowHigh - rowLow) / 2)
            else:
                rowLow = rowLow + math.ceil((rowHigh - rowLow) / 2)
        elif lettr == "L":
            if i == 10:
                colHigh = colHigh - math.ceil(colHigh - colLow) / 2
            else:
                colHigh = colHigh - math.ceil((colHigh - colLow) / 2)
        elif lettr == "R":
            if i == 10:
                colLow = colLow + math.ceil((colHigh - colLow) / 2)
            else:
                colLow = colLow + math.ceil((colHigh - colLow) / 2)
        addToLog(str(i+1) + " -- " + lettr + "    " + str(rowHigh) + "    " + str(rowLow) + "    " + str(colHigh) + "    " + str(colLow))
    thisSeatID = (rowLow* 8) + colLow
    addToLog("--- this seat id = " + str(thisSeatID))
    if thisSeatID > highestSeatID:
        highestSeatID = thisSeatID
        addToLog("new highest seat id = " + str(highestSeatID))
    passcount += 1
    lineCtr += 1
addToLog('There are ' + str(passcount) + ' boarding passes. The highest seat ID is ' + str(highestSeatID) + '.')
fl = open("5-1-log.txt", "w")
fl.write(log)
fl.close()
print('There are ' + str(passcount) + ' boarding passes. The highest seat ID is ' + str(highestSeatID) + '.')

And here’s my code for 2020 Day 5 part 2:

# Advent of Code 2020 Day 5 part 2 - What is the ID of your seat?
import math
log = ''
passcount = 0
highestSeatID = 0
lineCtr = 1
arrSeats = []

def addToLog(value):
    global log
    log += value + "\n"

with open("c:\\git\\advent-2020\\5.txt", "r") as f:
    seatlist = f.read().splitlines() 
f.close()

for j in range(0,1000):
    arrSeats.append(j)

for line in seatlist:
    rowHigh = 127
    rowLow = 0
    colHigh = 7
    colLow = 0
    addToLog("line " + str(lineCtr) + "    " + line)
    if line == "":
        break
    for i in range(0,10):
        # example : BFBBFFFLRR
        lettr = line[i]
        if lettr == "F":
            if i == 7:
                rowHigh = rowHigh - math.ceil(rowHigh - rowLow) / 2
            else:
                rowHigh = rowHigh - math.ceil((rowHigh - rowLow) / 2)
        elif lettr == "B":
            if i == 7:
                rowLow = rowLow + math.ceil((rowHigh - rowLow) / 2)
            else:
                rowLow = rowLow + math.ceil((rowHigh - rowLow) / 2)
        elif lettr == "L":
            if i == 10:
                colHigh = colHigh - math.ceil(colHigh - colLow) / 2
            else:
                colHigh = colHigh - math.ceil((colHigh - colLow) / 2)
        elif lettr == "R":
            if i == 10:
                colLow = colLow + math.ceil((colHigh - colLow) / 2)
            else:
                colLow = colLow + math.ceil((colHigh - colLow) / 2)
        addToLog(str(i+1) + " -- " + lettr + "    " + str(rowHigh) + "    " + str(rowLow) + "    " + str(colHigh) + "    " + str(colLow))
    thisSeatID = (rowLow* 8) + colLow
    addToLog("--- this seat id = " + str(thisSeatID))
    try:
        result = arrSeats.index(thisSeatID)
        arrSeats[result] = 'X'
    except:
        pass
        
    passcount += 1
    lineCtr += 1
for j in range(0, 1000):
    if arrSeats[j] != 'X':
        addToLog("Seat " + str(arrSeats[j]) + " is in the list")
addToLog('There are ' + str(passcount) + ' boarding passes. The highest seat ID is ' + str(highestSeatID) + '.')
fl = open("5-2-log.txt", "w")
fl.write(log)
fl.close()
print('There are ' + str(passcount) + ' boarding passes. The highest seat ID is ' + str(highestSeatID) + '.')

As always, if anyone sees something they could improve, I would appreciate hearing about it. Cheers!


You can leave a comment on this post here.