Given a list of boarding passes with the following shape:
BFBBFFFLRR
FFBFBBBLLL
FBFBFBFLLL
BBFFFBFLLR
FBFFBBFLRR
BBFBFFFLLL
Get to the boarding passes given the following rules
Boarding passes are divided like this;
xs ys
where xs = length 7
and ys = length 3
Where F means Front
and B means Back
and L means Left
and R means Right
Amount of seats 128
Amount of rows 8
The xs and ys are the same description to get to a number, where the following rules are kept:
Take first half: F, L
Take second half: B, R
I think the plan should be to:
- Split the list at length 7
- Send both lists to a generic function that takes a 'first' / 'second' char
- Calculate both lists
- zip them with (is there a function like that?) (FB * 8) + . RL. Where FB is the front / back no. and RL the left to right.
split / zip
...
So my gameplan worked, sort of. That step Calculate both lists
, I sort of skipped as I figured it wouldn't be super nice, we're basically calculating an integer. Why instantiate a list of integers every single time if you can just keep the indexes of the left and right hand side of the list? Well, a lot of manual figuring out to round the start and finish of the list... Took me way to long... On to no. two after a ☕.
4b comprises of finding a missing id in the middle of the list. I think we can assume that if the seats at the very front, or very back are missing, the whole row will be missing. I think I better start with just having a look at the outcome of data when I don't zip them but instead return the pairs of (Row, Seat).
Ok, so they were actually talking about seat ID's. Note to self: read better. I feel I cheated this one a little bit. Instead of going through the array and finding the missing variant, I actually just calculated what it would have to be given a list of sequential numbers. This would stop working the moment there are two missing items in the sequence.
I've also found that while I calculated the biggest seat properly, my algo did not work for items where the seat and or row ended up in the middle of the distribution. Rounding issue... Time to fix the first problem. I rewrote it to work with lists, but I would like it to simply calculate the number. Let's refactor the first one to do so...
I couldn't help myself, but wanting to do this in a better way than instantiating arrays all over the place. So I ended up refactoring the function to work with the numbers. I noticed I just had another pesky off-by-one error in there. Luckily, I could check / take from my full array instantiation example and reverse-engineer the solution.
The next thing I wanted to fix was the function that checked for missing elements. While this one was pretty quick, it would fail the moment there were more than one elements. The new function is supernice. I take two elements of the list, compare if the first one is the same as the next one, if it is, prepend it back to the rest of the list (O(1)
) and go again. If it isn't, I add the succ x
to the pred y
, adding all the elements in between the two items to the missing item list. Then also prepend the second element back to the rest. So all in all O(n)
and it really nicely 🎉.
An awesome thing I started doing today was using the REPL. I should have done that much earlier. By doing ghci foo.hs
, I could load up my file and run individual functions on some data. Supernice.