2022-12-02 16:56:31 -05:00
|
|
|
if __name__ == "__main__":
|
2022-12-03 14:00:20 -05:00
|
|
|
# Initialize empty list to store sums
|
|
|
|
sums = []
|
2022-12-02 16:56:31 -05:00
|
|
|
|
2022-12-03 14:00:20 -05:00
|
|
|
# Open input file
|
2022-12-02 16:56:31 -05:00
|
|
|
with open('input', 'r') as f:
|
2022-12-03 14:00:20 -05:00
|
|
|
# Initialize sum for current group of numbers
|
|
|
|
current_sum = 0
|
|
|
|
|
|
|
|
# Read lines from file
|
2022-12-02 16:56:31 -05:00
|
|
|
for line in f:
|
2022-12-03 14:00:20 -05:00
|
|
|
# If line is empty, append current sum to list and reset sum
|
2022-12-02 16:56:31 -05:00
|
|
|
if line.strip() == '':
|
2022-12-03 14:00:20 -05:00
|
|
|
sums.append(current_sum)
|
|
|
|
current_sum = 0
|
2022-12-02 16:56:31 -05:00
|
|
|
else:
|
2022-12-03 14:00:20 -05:00
|
|
|
# Add number from line to current sum
|
|
|
|
current_sum += int(line.strip())
|
|
|
|
|
|
|
|
# Add last sum to list
|
|
|
|
sums.append(current_sum)
|
|
|
|
|
|
|
|
# Sort sums in descending order
|
|
|
|
sums.sort(reverse=True)
|
2022-12-02 16:56:31 -05:00
|
|
|
|
2022-12-03 14:00:20 -05:00
|
|
|
# Print index and value of largest group of numbers
|
|
|
|
print(f'Index: 0\nValue: {sums[0]}')
|
2022-12-02 17:16:20 -05:00
|
|
|
|
2022-12-03 14:00:20 -05:00
|
|
|
# Calculate sum of top 3 groups of numbers
|
|
|
|
top3_sum = sums[0] + sums[1] + sums[2]
|
|
|
|
print(f'Top 3: {top3_sum}')
|
2022-12-02 17:16:20 -05:00
|
|
|
|