-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday01.py
More file actions
34 lines (26 loc) · 949 Bytes
/
Copy pathday01.py
File metadata and controls
34 lines (26 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import csv
import sys
import pandas as pd
class Calories:
"""Store the calorie count of each elf"""
def __init__(self, filename):
self.elves = []
with open(filename, newline='') as csv_file:
count = 0
reader = csv.reader(csv_file, quoting=csv.QUOTE_NONNUMERIC)
for row in reader:
if len(row) == 0:
self.elves.append(count)
count = 0
else:
count += int(row[0])
self.elves.append(count) # HACK: if count is 0 we don't know if that's the initial value or from the last CSV row.
def sum_max(self, top_count):
return pd.Series(self.elves).nlargest(top_count).sum()
def main(filename):
calories = Calories(filename)
print(f'Part 1: top elf is carrying {max(calories.elves)} calories')
print(f'Part 2: the top three elves are carrying {calories.sum_max(3)} calories')
if __name__ == "__main__":
# Call `python day_n.py <csv_file>`
main(sys.argv[1])