another day of coding
This commit is contained in:
56
13/input
Normal file
56
13/input
Normal file
@ -0,0 +1,56 @@
|
||||
Alice would gain 54 happiness units by sitting next to Bob.
|
||||
Alice would lose 81 happiness units by sitting next to Carol.
|
||||
Alice would lose 42 happiness units by sitting next to David.
|
||||
Alice would gain 89 happiness units by sitting next to Eric.
|
||||
Alice would lose 89 happiness units by sitting next to Frank.
|
||||
Alice would gain 97 happiness units by sitting next to George.
|
||||
Alice would lose 94 happiness units by sitting next to Mallory.
|
||||
Bob would gain 3 happiness units by sitting next to Alice.
|
||||
Bob would lose 70 happiness units by sitting next to Carol.
|
||||
Bob would lose 31 happiness units by sitting next to David.
|
||||
Bob would gain 72 happiness units by sitting next to Eric.
|
||||
Bob would lose 25 happiness units by sitting next to Frank.
|
||||
Bob would lose 95 happiness units by sitting next to George.
|
||||
Bob would gain 11 happiness units by sitting next to Mallory.
|
||||
Carol would lose 83 happiness units by sitting next to Alice.
|
||||
Carol would gain 8 happiness units by sitting next to Bob.
|
||||
Carol would gain 35 happiness units by sitting next to David.
|
||||
Carol would gain 10 happiness units by sitting next to Eric.
|
||||
Carol would gain 61 happiness units by sitting next to Frank.
|
||||
Carol would gain 10 happiness units by sitting next to George.
|
||||
Carol would gain 29 happiness units by sitting next to Mallory.
|
||||
David would gain 67 happiness units by sitting next to Alice.
|
||||
David would gain 25 happiness units by sitting next to Bob.
|
||||
David would gain 48 happiness units by sitting next to Carol.
|
||||
David would lose 65 happiness units by sitting next to Eric.
|
||||
David would gain 8 happiness units by sitting next to Frank.
|
||||
David would gain 84 happiness units by sitting next to George.
|
||||
David would gain 9 happiness units by sitting next to Mallory.
|
||||
Eric would lose 51 happiness units by sitting next to Alice.
|
||||
Eric would lose 39 happiness units by sitting next to Bob.
|
||||
Eric would gain 84 happiness units by sitting next to Carol.
|
||||
Eric would lose 98 happiness units by sitting next to David.
|
||||
Eric would lose 20 happiness units by sitting next to Frank.
|
||||
Eric would lose 6 happiness units by sitting next to George.
|
||||
Eric would gain 60 happiness units by sitting next to Mallory.
|
||||
Frank would gain 51 happiness units by sitting next to Alice.
|
||||
Frank would gain 79 happiness units by sitting next to Bob.
|
||||
Frank would gain 88 happiness units by sitting next to Carol.
|
||||
Frank would gain 33 happiness units by sitting next to David.
|
||||
Frank would gain 43 happiness units by sitting next to Eric.
|
||||
Frank would gain 77 happiness units by sitting next to George.
|
||||
Frank would lose 3 happiness units by sitting next to Mallory.
|
||||
George would lose 14 happiness units by sitting next to Alice.
|
||||
George would lose 12 happiness units by sitting next to Bob.
|
||||
George would lose 52 happiness units by sitting next to Carol.
|
||||
George would gain 14 happiness units by sitting next to David.
|
||||
George would lose 62 happiness units by sitting next to Eric.
|
||||
George would lose 18 happiness units by sitting next to Frank.
|
||||
George would lose 17 happiness units by sitting next to Mallory.
|
||||
Mallory would lose 36 happiness units by sitting next to Alice.
|
||||
Mallory would gain 76 happiness units by sitting next to Bob.
|
||||
Mallory would lose 34 happiness units by sitting next to Carol.
|
||||
Mallory would gain 37 happiness units by sitting next to David.
|
||||
Mallory would gain 40 happiness units by sitting next to Eric.
|
||||
Mallory would gain 18 happiness units by sitting next to Frank.
|
||||
Mallory would gain 7 happiness units by sitting next to George.
|
||||
12
13/input1
Normal file
12
13/input1
Normal file
@ -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.
|
||||
77
13/part1.py
Normal file
77
13/part1.py
Normal file
@ -0,0 +1,77 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import re
|
||||
|
||||
RE_HAPPY = re.compile(r'^(.*) would (gain|lose) (\d+) happiness units by sitting next to (.*).')
|
||||
|
||||
|
||||
class Person:
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.happiness = dict()
|
||||
|
||||
def addNeighborHappyness(self, name, value):
|
||||
self.happiness[name] = value
|
||||
|
||||
|
||||
def read_file(filename):
|
||||
file = open(filename, 'r')
|
||||
while True:
|
||||
line = file.readline()
|
||||
if not line:
|
||||
break
|
||||
yield line
|
||||
|
||||
|
||||
def getNeighborHappyness(persons, p1, p2):
|
||||
h1 = persons[p1].happiness[p2]
|
||||
h2 = persons[p2].happiness[p1]
|
||||
return h1 + h2
|
||||
|
||||
def computeHappiness(personList, personSitting, happyness):
|
||||
# empty list
|
||||
if len(personSitting) == 0:
|
||||
personSitting.insert(0, personList.keys()[0])
|
||||
computeHappiness(personList, personSitting, happyness)
|
||||
personSitting.pop(0)
|
||||
return
|
||||
|
||||
# full list
|
||||
if len(personList.keys()) == len(personSitting):
|
||||
p1 = personSitting[0]
|
||||
p2 = personSitting[len(personSitting)-1]
|
||||
happyness += getNeighborHappyness(personList, p1, p2)
|
||||
print "%010d %s" % (happyness, ", ".join(personSitting))
|
||||
happyness -= getNeighborHappyness(personList, p1, p2)
|
||||
return
|
||||
|
||||
firstPerson = personSitting[0]
|
||||
for neighbor in personList[firstPerson].happiness.keys():
|
||||
if neighbor in personSitting:
|
||||
continue
|
||||
personSitting.insert(0, neighbor)
|
||||
happyness += getNeighborHappyness(personList, firstPerson, neighbor)
|
||||
computeHappiness(personList, personSitting, happyness)
|
||||
personSitting.pop(0)
|
||||
happyness -= getNeighborHappyness(personList, firstPerson, neighbor)
|
||||
|
||||
|
||||
def main():
|
||||
persons = dict()
|
||||
for line in read_file('input'):
|
||||
match = re.search(RE_HAPPY, line)
|
||||
if not match:
|
||||
print "wrong operation syntax"
|
||||
if match.group(1) not in persons:
|
||||
persons[match.group(1)] = Person(match.group(1))
|
||||
happynnes = int(match.group(3))
|
||||
if match.group(2) == "lose":
|
||||
happynnes = happynnes * -1
|
||||
persons[match.group(1)].addNeighborHappyness(match.group(4), happynnes)
|
||||
|
||||
computeHappiness(persons, [], 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
83
13/part2.py
Normal file
83
13/part2.py
Normal file
@ -0,0 +1,83 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import re
|
||||
|
||||
RE_HAPPY = re.compile(r'^(.*) would (gain|lose) (\d+) happiness units by sitting next to (.*).')
|
||||
|
||||
|
||||
class Person:
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
self.happiness = dict()
|
||||
|
||||
def addNeighborHappyness(self, name, value):
|
||||
self.happiness[name] = value
|
||||
|
||||
|
||||
def read_file(filename):
|
||||
file = open(filename, 'r')
|
||||
while True:
|
||||
line = file.readline()
|
||||
if not line:
|
||||
break
|
||||
yield line
|
||||
|
||||
|
||||
def getNeighborHappyness(persons, p1, p2):
|
||||
h1 = persons[p1].happiness[p2]
|
||||
h2 = persons[p2].happiness[p1]
|
||||
return h1 + h2
|
||||
|
||||
def computeHappiness(personList, personSitting, happyness):
|
||||
# empty list
|
||||
if len(personSitting) == 0:
|
||||
personSitting.insert(0, personList.keys()[0])
|
||||
computeHappiness(personList, personSitting, happyness)
|
||||
personSitting.pop(0)
|
||||
return
|
||||
|
||||
# full list
|
||||
if len(personList.keys()) == len(personSitting):
|
||||
p1 = personSitting[0]
|
||||
p2 = personSitting[len(personSitting)-1]
|
||||
happyness += getNeighborHappyness(personList, p1, p2)
|
||||
print "%010d %s" % (happyness, ", ".join(personSitting))
|
||||
happyness -= getNeighborHappyness(personList, p1, p2)
|
||||
return
|
||||
|
||||
firstPerson = personSitting[0]
|
||||
for neighbor in personList[firstPerson].happiness.keys():
|
||||
if neighbor in personSitting:
|
||||
continue
|
||||
personSitting.insert(0, neighbor)
|
||||
happyness += getNeighborHappyness(personList, firstPerson, neighbor)
|
||||
computeHappiness(personList, personSitting, happyness)
|
||||
personSitting.pop(0)
|
||||
happyness -= getNeighborHappyness(personList, firstPerson, neighbor)
|
||||
|
||||
|
||||
def main():
|
||||
persons = dict()
|
||||
for line in read_file('input'):
|
||||
match = re.search(RE_HAPPY, line)
|
||||
if not match:
|
||||
print "wrong operation syntax"
|
||||
if match.group(1) not in persons:
|
||||
persons[match.group(1)] = Person(match.group(1))
|
||||
happynnes = int(match.group(3))
|
||||
if match.group(2) == "lose":
|
||||
happynnes = happynnes * -1
|
||||
persons[match.group(1)].addNeighborHappyness(match.group(4), happynnes)
|
||||
|
||||
me = Person('me')
|
||||
for person in persons.keys():
|
||||
persons[person].addNeighborHappyness('me', 0)
|
||||
me.addNeighborHappyness(person, 0)
|
||||
persons['me'] = me
|
||||
|
||||
computeHappiness(persons, [], 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user