file = "data/day1.txt"
= open(file)
f = f.read()
lines
f.close()
= lines.split('\n')
lines 10] lines[:
['5474', '4920', '5381', '8650', '11617', '7193', '8161', '', '10747', '5855']
Trying to practice with using OOP and classes inside of python.
The idea that I am going to be aiming for, is to have a class to represent an elf, and then also a class that contains all of the elves together.
Unsure really about how classes work, so this will be an experiment!
Lets first get all of the data inside and have a look at it.
I’ll want a class that is an elf, which at least to begin with will have an id number to identify which elf it is, but also a place to store which snacks they are carrying, and a function to count up the calories of all of the snacks they are carrying.
Now we want a class than can hold multiple elves, with a function to add an elf, and a function to find which elf has the most calories.
class Mischief:
def __init__(self):
self.elves = []
def add_elf(self):
self.elves.append(Elf(len(self.elves)))
def add_elves_and_snacks(self, lines):
new_elf = True
counter = 0
for snack in lines:
if new_elf:
self.add_elf()
new_elf = False
if snack != "":
group.elves[counter].add_snack(int(snack))
else:
new_elf = True
counter += 1
def get_elf_max_cal(self):
max_elf = self.elves[0]
for elf in self.elves:
if elf.cal_total() > max_elf.cal_total():
max_elf = elf
print(f"Elf No. {max_elf.id} with {max_elf.cal_total()} calories.")
return max_elf
# setup the mischief of elves
group = Mischief()
# add the elves and their snacks
# I feel like this should be part of the setup, but I can't currenlty figure out
# how to get it working
group.add_elves_and_snacks(lines)
# find the elf with the most snacks!
group.get_elf_max_cal()
Elf No. 30 with 66306 calories.
<__main__.Elf at 0x7fd131ffc580>
Find the top 3 elves, and how many calories total they all are carrying.
all_snack_totals = [elf.cal_total() for elf in group.elves]
all_snack_totals.sort(reverse=True)
top_3 = sum(all_snack_totals[:3])
print(f"The 3 elves with the most snacks have a combined {top_3} calories.")
The 3 elves with the most snacks have a combined 195292 calories.
While this finds the top 3 elf snack calories and adds them together, it’s unclear where those totals are coming from. Thant’s something I’d like to improve, but this is where I will leave this part for the python component.