mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Move 2015 out of the way.
This commit is contained in:
56
2015/day-13/input.txt
Normal file
56
2015/day-13/input.txt
Normal file
@@ -0,0 +1,56 @@
|
||||
Alice would lose 2 happiness units by sitting next to Bob.
|
||||
Alice would lose 62 happiness units by sitting next to Carol.
|
||||
Alice would gain 65 happiness units by sitting next to David.
|
||||
Alice would gain 21 happiness units by sitting next to Eric.
|
||||
Alice would lose 81 happiness units by sitting next to Frank.
|
||||
Alice would lose 4 happiness units by sitting next to George.
|
||||
Alice would lose 80 happiness units by sitting next to Mallory.
|
||||
Bob would gain 93 happiness units by sitting next to Alice.
|
||||
Bob would gain 19 happiness units by sitting next to Carol.
|
||||
Bob would gain 5 happiness units by sitting next to David.
|
||||
Bob would gain 49 happiness units by sitting next to Eric.
|
||||
Bob would gain 68 happiness units by sitting next to Frank.
|
||||
Bob would gain 23 happiness units by sitting next to George.
|
||||
Bob would gain 29 happiness units by sitting next to Mallory.
|
||||
Carol would lose 54 happiness units by sitting next to Alice.
|
||||
Carol would lose 70 happiness units by sitting next to Bob.
|
||||
Carol would lose 37 happiness units by sitting next to David.
|
||||
Carol would lose 46 happiness units by sitting next to Eric.
|
||||
Carol would gain 33 happiness units by sitting next to Frank.
|
||||
Carol would lose 35 happiness units by sitting next to George.
|
||||
Carol would gain 10 happiness units by sitting next to Mallory.
|
||||
David would gain 43 happiness units by sitting next to Alice.
|
||||
David would lose 96 happiness units by sitting next to Bob.
|
||||
David would lose 53 happiness units by sitting next to Carol.
|
||||
David would lose 30 happiness units by sitting next to Eric.
|
||||
David would lose 12 happiness units by sitting next to Frank.
|
||||
David would gain 75 happiness units by sitting next to George.
|
||||
David would lose 20 happiness units by sitting next to Mallory.
|
||||
Eric would gain 8 happiness units by sitting next to Alice.
|
||||
Eric would lose 89 happiness units by sitting next to Bob.
|
||||
Eric would lose 69 happiness units by sitting next to Carol.
|
||||
Eric would lose 34 happiness units by sitting next to David.
|
||||
Eric would gain 95 happiness units by sitting next to Frank.
|
||||
Eric would gain 34 happiness units by sitting next to George.
|
||||
Eric would lose 99 happiness units by sitting next to Mallory.
|
||||
Frank would lose 97 happiness units by sitting next to Alice.
|
||||
Frank would gain 6 happiness units by sitting next to Bob.
|
||||
Frank would lose 9 happiness units by sitting next to Carol.
|
||||
Frank would gain 56 happiness units by sitting next to David.
|
||||
Frank would lose 17 happiness units by sitting next to Eric.
|
||||
Frank would gain 18 happiness units by sitting next to George.
|
||||
Frank would lose 56 happiness units by sitting next to Mallory.
|
||||
George would gain 45 happiness units by sitting next to Alice.
|
||||
George would gain 76 happiness units by sitting next to Bob.
|
||||
George would gain 63 happiness units by sitting next to Carol.
|
||||
George would gain 54 happiness units by sitting next to David.
|
||||
George would gain 54 happiness units by sitting next to Eric.
|
||||
George would gain 30 happiness units by sitting next to Frank.
|
||||
George would gain 7 happiness units by sitting next to Mallory.
|
||||
Mallory would gain 31 happiness units by sitting next to Alice.
|
||||
Mallory would lose 32 happiness units by sitting next to Bob.
|
||||
Mallory would gain 95 happiness units by sitting next to Carol.
|
||||
Mallory would gain 91 happiness units by sitting next to David.
|
||||
Mallory would lose 66 happiness units by sitting next to Eric.
|
||||
Mallory would lose 75 happiness units by sitting next to Frank.
|
||||
Mallory would lose 99 happiness units by sitting next to George.
|
||||
51
2015/day-13/solution.py
Normal file
51
2015/day-13/solution.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from __future__ import print_function
|
||||
import fileinput
|
||||
import re
|
||||
from itertools import permutations
|
||||
|
||||
linePattern = re.compile(r"(\w+) would (gain|lose) (\d+) happiness units by sitting next to (\w+)\.")
|
||||
|
||||
ratings = {}
|
||||
|
||||
|
||||
# Read the input by regex
|
||||
for line in fileinput.input():
|
||||
match = linePattern.match(line)
|
||||
person = match.group(1)
|
||||
if person not in ratings:
|
||||
ratings[person] = {}
|
||||
|
||||
value = int(match.group(3))
|
||||
if match.group(2) == "lose":
|
||||
value *= -1
|
||||
|
||||
target = match.group(4)
|
||||
ratings[person][target] = value
|
||||
|
||||
maxHappiness = 0
|
||||
maxWithMe = 0
|
||||
|
||||
# Compute happiness with everyone present
|
||||
for permutation in permutations(list(ratings.keys())):
|
||||
happiness = 0
|
||||
withMe = 0
|
||||
|
||||
for i in range(len(permutation)):
|
||||
target = permutation[i]
|
||||
neighbour1 = permutation[(i + 1) % len(permutation)]
|
||||
neighbour2 = permutation[(i - 1 + len(permutation)) % len(permutation)]
|
||||
|
||||
happiness += ratings[target][neighbour1]
|
||||
if i < len(permutation) - 1:
|
||||
withMe += ratings[target][neighbour1]
|
||||
|
||||
happiness += ratings[target][neighbour2]
|
||||
if i > 0:
|
||||
withMe += ratings[target][neighbour2]
|
||||
|
||||
maxHappiness = max(maxHappiness, happiness)
|
||||
maxWithMe = max(maxWithMe, withMe)
|
||||
|
||||
|
||||
print(maxHappiness, maxWithMe)
|
||||
|
||||
Reference in New Issue
Block a user