add solution for day 1

This commit is contained in:
aaron
2022-12-03 13:29:10 +01:00
parent 4721bbd87f
commit 4fd095409b

View File

@@ -3,34 +3,39 @@ from aocd import submit
class Elf: class Elf:
''' '''
Elf object object which holds a a list of calories
carried by this particular elf
''' '''
calories = list() calories = list()
def __init__(self, numbers: list) -> None: def __init__(self, numbers: list) -> None:
''' '''
data class constructor constructor
''' '''
self.calories = numbers.copy() self.calories = numbers.copy()
def __lt__(self, other) -> bool:
'''
compare if sum of calorie is less than other
'''
return (self.sum_calories() < other.sum_calories())
def __repr__(self) -> str:
'''
print string representation of object
'''
return f'{str(self.calories)}'
def sum_calories(self) -> int: def sum_calories(self) -> int:
''' '''
calculate the total calories carried by this elf calculate the total calories carried by this elf
''' '''
return sum(self.calories) return sum(self.calories)
def __lt__(self, other) -> bool:
'''
check if sum calories is less than other
'''
return (self.sum_calories() < other.sum_calories())
def __repr__(self) -> str:
return f'{str(self.calories)}'
def parse_input(data: list) -> list: def parse_input(data: list) -> list:
''' '''
parses the input data and generates a list of elfs parses the input data and generates a list of elfs
from the input data string
''' '''
# split input string into list # split input string into list
splits = data.split('\n') splits = data.split('\n')
@@ -49,9 +54,9 @@ def parse_input(data: list) -> list:
if __name__ == "__main__": if __name__ == "__main__":
# get puzzle and parse data # get puzzle and parse data
puzzle = Puzzle(year=2022, day=1) puzzle = Puzzle(year=2022, day=1)
data = puzzle.input_data
elfs = parse_input(data) # get a list of elfs and sort based on calories
# sort elfs based on calories elfs = parse_input(puzzle.input_data)
elfs = sorted(elfs, reverse=True) elfs = sorted(elfs, reverse=True)
# part a: submit biggest number of calories # part a: submit biggest number of calories
@@ -60,8 +65,6 @@ if __name__ == "__main__":
submit(answer, part='a', day=1, year=2022) submit(answer, part='a', day=1, year=2022)
# part b: submit the calorie sum of the top 3 elfs # part b: submit the calorie sum of the top 3 elfs
answer = 0 answer = sum([ elf.sum_calories() for elf in elfs[:3]])
for elf in elfs[:3]:
answer += elf.sum_calories()
print(f'answer part b: {answer}') print(f'answer part b: {answer}')
submit(answer, part='b', day=1, year=2022) submit(answer, part='b', day=1, year=2022)