Advent of Code 2015 day 13 part 1&2: solved
This commit is contained in:
parent
d6bccb2e1f
commit
dce8869d9f
|
|
@ -0,0 +1,56 @@
|
|||
Alice would gain 2 happiness units by sitting next to Bob.
|
||||
Alice would gain 26 happiness units by sitting next to Carol.
|
||||
Alice would lose 82 happiness units by sitting next to David.
|
||||
Alice would lose 75 happiness units by sitting next to Eric.
|
||||
Alice would gain 42 happiness units by sitting next to Frank.
|
||||
Alice would gain 38 happiness units by sitting next to George.
|
||||
Alice would gain 39 happiness units by sitting next to Mallory.
|
||||
Bob would gain 40 happiness units by sitting next to Alice.
|
||||
Bob would lose 61 happiness units by sitting next to Carol.
|
||||
Bob would lose 15 happiness units by sitting next to David.
|
||||
Bob would gain 63 happiness units by sitting next to Eric.
|
||||
Bob would gain 41 happiness units by sitting next to Frank.
|
||||
Bob would gain 30 happiness units by sitting next to George.
|
||||
Bob would gain 87 happiness units by sitting next to Mallory.
|
||||
Carol would lose 35 happiness units by sitting next to Alice.
|
||||
Carol would lose 99 happiness units by sitting next to Bob.
|
||||
Carol would lose 51 happiness units by sitting next to David.
|
||||
Carol would gain 95 happiness units by sitting next to Eric.
|
||||
Carol would gain 90 happiness units by sitting next to Frank.
|
||||
Carol would lose 16 happiness units by sitting next to George.
|
||||
Carol would gain 94 happiness units by sitting next to Mallory.
|
||||
David would gain 36 happiness units by sitting next to Alice.
|
||||
David would lose 18 happiness units by sitting next to Bob.
|
||||
David would lose 65 happiness units by sitting next to Carol.
|
||||
David would lose 18 happiness units by sitting next to Eric.
|
||||
David would lose 22 happiness units by sitting next to Frank.
|
||||
David would gain 2 happiness units by sitting next to George.
|
||||
David would gain 42 happiness units by sitting next to Mallory.
|
||||
Eric would lose 65 happiness units by sitting next to Alice.
|
||||
Eric would gain 24 happiness units by sitting next to Bob.
|
||||
Eric would gain 100 happiness units by sitting next to Carol.
|
||||
Eric would gain 51 happiness units by sitting next to David.
|
||||
Eric would gain 21 happiness units by sitting next to Frank.
|
||||
Eric would gain 55 happiness units by sitting next to George.
|
||||
Eric would lose 44 happiness units by sitting next to Mallory.
|
||||
Frank would lose 48 happiness units by sitting next to Alice.
|
||||
Frank would gain 91 happiness units by sitting next to Bob.
|
||||
Frank would gain 8 happiness units by sitting next to Carol.
|
||||
Frank would lose 66 happiness units by sitting next to David.
|
||||
Frank would gain 97 happiness units by sitting next to Eric.
|
||||
Frank would lose 9 happiness units by sitting next to George.
|
||||
Frank would lose 92 happiness units by sitting next to Mallory.
|
||||
George would lose 44 happiness units by sitting next to Alice.
|
||||
George would lose 25 happiness units by sitting next to Bob.
|
||||
George would gain 17 happiness units by sitting next to Carol.
|
||||
George would gain 92 happiness units by sitting next to David.
|
||||
George would lose 92 happiness units by sitting next to Eric.
|
||||
George would gain 18 happiness units by sitting next to Frank.
|
||||
George would gain 97 happiness units by sitting next to Mallory.
|
||||
Mallory would gain 92 happiness units by sitting next to Alice.
|
||||
Mallory would lose 96 happiness units by sitting next to Bob.
|
||||
Mallory would lose 51 happiness units by sitting next to Carol.
|
||||
Mallory would lose 81 happiness units by sitting next to David.
|
||||
Mallory would gain 31 happiness units by sitting next to Eric.
|
||||
Mallory would lose 73 happiness units by sitting next to Frank.
|
||||
Mallory would lose 89 happiness units by sitting next to George.
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
--- 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 44.
|
||||
|
||||
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?
|
||||
|
||||
Your puzzle answer was 733.
|
||||
|
||||
--- Part Two ---
|
||||
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?
|
||||
|
||||
Your puzzle answer was 725.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
Bob would gain 0 happiness units by sitting next to Me
|
||||
Alice would gain 0 happiness units by sitting next to Me
|
||||
Carol would gain 0 happiness units by sitting next to Me
|
||||
David would gain 0 happiness units by sitting next to Me
|
||||
Eric would gain 0 happiness units by sitting next to Me
|
||||
Frank would gain 0 happiness units by sitting next to Me
|
||||
George would gain 0 happiness units by sitting next to Me
|
||||
Mallory would gain 0 happiness units by sitting next to Me
|
||||
Me would gain 0 happiness units by sitting next to Alice
|
||||
Me would gain 0 happiness units by sitting next to Bob
|
||||
Me would gain 0 happiness units by sitting next to Carol
|
||||
Me would gain 0 happiness units by sitting next to David
|
||||
Me would gain 0 happiness units by sitting next to Eric
|
||||
Me would gain 0 happiness units by sitting next to Frank
|
||||
Me would gain 0 happiness units by sitting next to George
|
||||
Me would gain 0 happiness units by sitting next to Mallory
|
||||
|
|
@ -0,0 +1,80 @@
|
|||
import math
|
||||
|
||||
|
||||
def load_puzzle_input(_input: list) -> list:
|
||||
output: list = []
|
||||
for i in _input:
|
||||
tmp_i = i.replace('.', '').strip().split(' ')
|
||||
person, other_person = tmp_i[0], tmp_i[-1]
|
||||
points = int(tmp_i[3])
|
||||
if "lose" in i:
|
||||
points = -points
|
||||
output.append((person, other_person, points))
|
||||
return output
|
||||
|
||||
|
||||
def generate_lookup_table(_input: list) -> dict:
|
||||
lookup: dict = {}
|
||||
for i in _input:
|
||||
person, other_person, points = i
|
||||
lookup[person + "_" + other_person] = points
|
||||
return lookup
|
||||
|
||||
|
||||
def get_all_possible_combinations(_input: list, person: str, first_person: str, number_other_persons: int,
|
||||
lookup_table: dict) -> list:
|
||||
possible_next_persons = []
|
||||
for i in _input:
|
||||
if (not i[1] == person) or i[1] == first_person:
|
||||
possible_next_persons.append(i)
|
||||
|
||||
output = []
|
||||
for i in possible_next_persons:
|
||||
_person, other_person, change = i
|
||||
if len(possible_next_persons) == number_other_persons:
|
||||
if _person == person and other_person == first_person:
|
||||
change += lookup_table[first_person + "_" + person]
|
||||
output.append(((person, first_person), change))
|
||||
elif _person == person and other_person != first_person:
|
||||
for p in get_all_possible_combinations(possible_next_persons, other_person, first_person,
|
||||
number_other_persons, lookup_table):
|
||||
persons, total_change = p
|
||||
persons = list(persons)
|
||||
persons.insert(0, person)
|
||||
total_change += change + lookup_table[other_person + "_" + _person]
|
||||
output.append((persons, total_change))
|
||||
|
||||
return output
|
||||
|
||||
|
||||
def get_perfect_seating(_input: list) -> int:
|
||||
person, other_person, starting_change = _input[0]
|
||||
lookup = generate_lookup_table(_input)
|
||||
possible_combinations = []
|
||||
for p in get_all_possible_combinations(_input, person, person, int(math.sqrt(len(_input))), lookup):
|
||||
possible_combinations.append(p)
|
||||
max_change = None
|
||||
for combination in possible_combinations:
|
||||
persons, total_change = combination
|
||||
if max_change is None or total_change > max_change:
|
||||
max_change = total_change
|
||||
return max_change
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
assert load_puzzle_input(["Mallory would lose 89 happiness units by sitting next to George."]) == [
|
||||
('Mallory', 'George', -89)]
|
||||
test_input = open("test_input.txt", "r").read().splitlines()
|
||||
test_input = load_puzzle_input(test_input)
|
||||
assert get_perfect_seating(test_input) == 330, "Error: Example failed"
|
||||
print("All test passed")
|
||||
|
||||
puzzle_input = open("input.txt", "r").read().splitlines()
|
||||
solution1 = get_perfect_seating(load_puzzle_input(puzzle_input))
|
||||
print("solution: ", get_perfect_seating(load_puzzle_input(puzzle_input)))
|
||||
|
||||
puzzle_input += open("self_input.txt", "r").read().splitlines()
|
||||
converted_input = load_puzzle_input(puzzle_input)
|
||||
print(converted_input)
|
||||
print("Part2: ", get_perfect_seating(converted_input))
|
||||
print("solution: ", )
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
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.
|
||||
Loading…
Reference in New Issue