solved day 1 challenge
This commit is contained in:
1
.env
1
.env
@@ -1 +0,0 @@
|
|||||||
AOC_SESSION_TOKEN="53616c7465645f5febd530e486345671133bc1c82b7ff5ed1072d0bc6cc6d82d90a9be2d91881d069c28682c54abcb9427a07cc429f65d26de34d465e8e1ac7b"
|
|
||||||
@@ -1,5 +1,67 @@
|
|||||||
from aocd import data
|
from aocd.models import Puzzle
|
||||||
|
from aocd import submit
|
||||||
|
|
||||||
AOC_COOKIE = os.environ.get('AOC_SESSION_TOKEN', 'default')
|
class Elf:
|
||||||
|
'''
|
||||||
|
Elf object
|
||||||
|
'''
|
||||||
|
calories = list()
|
||||||
|
|
||||||
|
def __init__(self, numbers: list) -> None:
|
||||||
|
'''
|
||||||
|
data class constructor
|
||||||
|
'''
|
||||||
|
self.calories = numbers.copy()
|
||||||
|
|
||||||
|
def sum_calories(self) -> int:
|
||||||
|
'''
|
||||||
|
calculate the total calories carried by this elf
|
||||||
|
'''
|
||||||
|
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:
|
||||||
|
'''
|
||||||
|
parses the input data and generates a list of elfs
|
||||||
|
'''
|
||||||
|
# split input string into list
|
||||||
|
splits = data.split('\n')
|
||||||
|
# split list into sublists
|
||||||
|
numbers = list()
|
||||||
|
tmp = list()
|
||||||
|
for number in splits:
|
||||||
|
if not number:
|
||||||
|
numbers.append(tmp.copy())
|
||||||
|
tmp.clear()
|
||||||
|
else:
|
||||||
|
tmp.append(int(number))
|
||||||
|
# elf object for each sublist
|
||||||
|
return [ Elf(data) for data in numbers ]
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# get puzzle and parse data
|
||||||
|
puzzle = Puzzle(year=2022, day=1)
|
||||||
|
data = puzzle.input_data
|
||||||
|
elfs = parse_input(data)
|
||||||
|
# sort elfs based on calories
|
||||||
|
elfs = sorted(elfs, reverse=True)
|
||||||
|
|
||||||
|
# part a: submit biggest number of calories
|
||||||
|
answer = elfs[0].sum_calories()
|
||||||
|
print(f'answer part a: {answer}')
|
||||||
|
submit(answer, part='a', day=1, year=2022)
|
||||||
|
|
||||||
|
# part b: submit the calorie sum of the top 3 elfs
|
||||||
|
answer = 0
|
||||||
|
for elf in elfs[:3]:
|
||||||
|
answer += elf.sum_calories()
|
||||||
|
print(f'answer part b: {answer}')
|
||||||
|
submit(answer, part='b', day=1, year=2022)
|
||||||
|
|||||||
Reference in New Issue
Block a user