AoC 2015 Puzzle 13

Info

I solved this puzzle. You can find the code here, but I have yet to write a good explanation of the solution here. I will do this shortly, thank you for your patience.

Day 13: Knights of the Dinner Table

In years past, the holiday feast with your family hasn't gone so well. Not everyone gets along! This year, you resolve, will be different. You're going to find the optimal seating arrangement and avoid all those awkward conversations.

You start by writing up a list of everyone invited and the amount their happiness would increase or decrease if they were to find themselves sitting next to each other person. You have a circular table that will be just big enough to fit everyone comfortably, and so each person will have exactly two neighbors.

For example, suppose you have only four attendees planned, and you calculate their potential happiness as follows:

  • Alice would gain 54 happiness units by sitting next to Bob.
  • Alice would lose 79 happiness units by sitting next to Carol.
  • Alice would lose 2 happiness units by sitting next to David.
  • Bob would gain 83 happiness units by sitting next to Alice.
  • Bob would lose 7 happiness units by sitting next to Carol.
  • Bob would lose 63 happiness units by sitting next to David.
  • Carol would lose 62 happiness units by sitting next to Alice.
  • Carol would gain 60 happiness units by sitting next to Bob.
  • Carol would gain 55 happiness units by sitting next to David.
  • David would gain 46 happiness units by sitting next to Alice.
  • David would lose 7 happiness units by sitting next to Bob.
  • David would gain 41 happiness units by sitting next to Carol.

Then, if you seat Alice next to David, Alice would lose 2 happiness units (because David talks so much), but David would gain 46 happiness units (because Alice is such a good listener), for a total change of

If you continue around the table, you could then seat Bob next to Alice (Bob gains 83, Alice gains 54). Finally, seat Carol, who sits next to Bob (Carol gains 60, Bob loses 7) and David (Carol gains 55, David gains 41). The arrangement looks like this:

+41 +46 +55 David -2 Carol Alice +60 Bob +54 -7 +83

After trying every other seating arrangement in this hypothetical scenario, you find that this one is the most optimal, with a total change in happiness of 330.

What is the total change in happiness for the optimal seating arrangement of the actual guest list?

Part 2

In all the commotion, you realize that you forgot to seat yourself. At this point, you're pretty apathetic toward the whole thing, and your happiness wouldn't really go up or down regardless of who you sit next to. You assume everyone else would be just as ambivalent about sitting next to you, too.

So, add yourself to the list, and give all happiness relationships that involve you a score of 0.

What is the total change in happiness for the optimal seating arrangement that actually includes yourself?

private <T> void swap(T[] input, int a, int b) {
    T tmp = input[a];
    input[a] = input[b];
    input[b] = tmp;
}

public <T> List<T[]> permuteAllIterative(int n, T[] elements) {
    List<T[]> permutations = new ArrayList<>();
    int[] indexes = new int[n];
    for (int i = 0; i < n; i++) {
        indexes[i] = 0;
    }

    permutations.add(elements.clone());

    int i = 0;
    while (i < n) {
        if (indexes[i] < i) {
            swap(elements, i % 2 == 0 ? 0 : indexes[i], i);
            permutations.add(elements.clone());
            indexes[i]++;
            i = 0;
        } else {
            indexes[i] = 0;
            i++;
        }
    }

    return permutations;
}

// Map of who loves/hates whom
Map<String, Map<String, Integer>> loveHate = new HashMap<>();
// Maintain a list of guests
List<String> guests = new ArrayList<>();

public void processLoveHate(List<String> input) {
    // Parse the instructios into our map
    for (String instruction : input) {
        // get rid of the . at the end
        String[] parts = instruction.substring(0, instruction.length() - 1).split(" ");
        Map<String, Integer> currentLoveHate = loveHate.getOrDefault(parts[0], new HashMap<>());
        Integer value = Integer.parseInt(parts[3]);

        currentLoveHate.put(parts[10], parts[2].equals("gain") ? value : -value);

        if (!guests.contains(parts[0]))
            guests.add(parts[0]);

        loveHate.put(parts[0], currentLoveHate);
    }

}

public Integer getChange(String ben, String jerry) {
    Integer change = 0;
    change += loveHate.getOrDefault(ben, new HashMap<>()).getOrDefault(jerry, 0);
    change += loveHate.getOrDefault(jerry, new HashMap<>()).getOrDefault(ben, 0);
    return change;
}

public int determineHappiness() {
    List<String[]> permutations = permuteAllIterative(guests.size(), guests.toArray(new String[0]));

    int mostHappy = 0;
    for (String[] p : permutations) {
        int happy = 0;
        for (int i = 0; i < p.length - 1; i++) {
            happy += getChange(p[i], p[i + 1]);
        }
        happy += getChange(p[0], p[p.length - 1]);

        if (happy > mostHappy)
            mostHappy = happy;
    }
    return mostHappy;
}

@Override
public String part1(List<String> input) {
    processLoveHate(input);

    return String.valueOf(determineHappiness());
}

@Override
public String part2(List<String> input) {
    processLoveHate(input);
    guests.add("Arjen");

    return String.valueOf(determineHappiness());
}