Part 1
For this puzzle we need the find the table configuration leading to the most happiness. Each guest produces happiness depending on who they are sitting next to.
We start by parsing the input text file using regex to get the list of all guests and their happiness gain. To find the optimal table configuration, we do a brute force search over all possible guest orders. We only have 8 guests, this means 8! = 40320 possible configurations, so a search is feasible. For each configuration we calculate the happiness resulting from this guest order. We iterate over each element in the order and add the happiness this guest gets from each neighbour. To capture the circularity of the table, we also add happiness from the first and last guests sitting next to each other. We could further optimize the search since the happiness from the order A, B, C is the same as from the order C, A, B. But this script ran fast enough, so no optimization was needed.
Part 2
In the second part, we have an additional guest who gets no happiness from their neighbour.
For this we add an additional guest after the parsing of the input and run the brute force search from part 1 again. The 9! = 362880 possibilities are also quickly evaluated.
No comments:
Post a Comment