DEV Community

DoctorLai
DoctorLai

Posted on • Updated on

Avent of Code - Day 01 - Calorie Counting

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 - Calorie Counting

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])
Enter fullscreen mode Exit fullscreen mode

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])
Enter fullscreen mode Exit fullscreen mode

Steem to the Moon!

Top comments (0)