Avent of Code - Day 01 - Find the Largest Sum of Groups

in adventofcode •  2 years ago 

Advent of Code occurs at Dec 01 to 25 where each day, you will need to solve a puzzle. It is Festival and the problem statement is mostly related to Christmas.

image.png

You can join my leaderboard by entering this code 1019229-a302d391

Day 01 - Finding the Largest Sum of a Group

The first few days are usually simple/easy/trivial. You don't need to submit code, but you would probably need to write code to get the answer. Some people can solve easy ones in Excel.

Q1 - the largest sum

file1 = open("0.txt", "r")
ans = []
cur = 0
while True:
        line = file1.readline()
        if not line:
                break
        line = line.strip()
        if not line:
                ans.append(cur)
                cur = 0
        else:
                cur += int(line)

ans.sort()
print(ans[-1] + ans[-2] + ans[-3])

Q2 - the largest 3 sums

file1 = open("0.txt", "r")
ans = []
cur = 0
while True:
        line = file1.readline()
        if not line:
                break
        line = line.strip()
        if not line:
                ans.append(cur)
                cur = 0
        else:
                cur += int(line)

ans.sort()
print(ans[-1] + ans[-2] + ans[-3])

Steem to the Moon!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!