diff --git a/day1/day1.py b/day1/day1.py index 4d36409..a62033d 100644 --- a/day1/day1.py +++ b/day1/day1.py @@ -1,23 +1,32 @@ if __name__ == "__main__": - elves = {} + # Initialize empty list to store sums + sums = [] + # Open input file with open('input', 'r') as f: - elf = 0 - elves[0] = 0 + # Initialize sum for current group of numbers + current_sum = 0 + + # Read lines from file for line in f: + # If line is empty, append current sum to list and reset sum if line.strip() == '': - elf += 1 - elves[elf] = 0 + sums.append(current_sum) + current_sum = 0 else: - elves[elf] += int(line.strip()) + # Add number from line to current sum + current_sum += int(line.strip()) - largest = max(elves, key=elves.get) - print(f'Index: {largest}\nValue: {elves[largest]}') + # Add last sum to list + sums.append(current_sum) - top3 = 0 - for i in range(3): - largest = max(elves, key=elves.get) - top3 += elves[largest] - del elves[largest] + # Sort sums in descending order + sums.sort(reverse=True) + + # Print index and value of largest group of numbers + print(f'Index: 0\nValue: {sums[0]}') + + # Calculate sum of top 3 groups of numbers + top3_sum = sums[0] + sums[1] + sums[2] + print(f'Top 3: {top3_sum}') - print(f'Top 3: {top3}')