Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Advent of Code
#21
well, since break ends the loop, how do i restart it ? do i need to enclose all of it in a while loop ?
Reply
#22
Kind of a question that answers itself. Don't break.

You already have enough loops. no need for more.
Reply
#23
okay, i'm lost

i know you guys try to give me the best advice...but i think i need to back track a little and try do some simpler stuff (this should've been fairly simple) but what can i do\
Reply
#24
It will be hard to find a challenge simpler than this one.

A Compute total for a group of numbers.
B Repeat for multiple groups.
C Print the largest total.

You already did A.
B is just repeating A.
C is the trickiest part. Ignore for now as it will be easier when you have done A and B

To start computing a total what do you have to do? You already did this for the first group. It's not like your program has a lot of code. What step(s) did you perform in your existing program to prepare for computing the total? When you start computing the total for the second group you'll need to perform these same steps.
Reply
#25
One solution is in this Thread,just need to put it together.
Put your input instead of test.txt.
sum_tot = []
with open("test.txt") as file:
    sum_list = []
    for line in file:
        if line != "\n":
            sum_list.append(int(line))
        else:
            sum_tot.append(sum(sum_list))
            sum_list = []

print(max(sum_tot))
astral_travel Wrote:but i think i need to back track a little and try do some simpler stuff (this should've been fairly simple) but what can i do
Yes the Advent of code can be tricky in new to Python.
Look at something like CheckiO where task is much more beginner friendly.
Reply
#26
thank you Dean and snippsat,

i understood a bit more,

there is one thing i don't understand and which i'm stuck on - is how to sum up the group, without using a list,

snippsat did it with line 8
sum_tot.append(sum(sum_list))
using sum

one element in my mind - if there is no way to save the groups's sum side by side in a list - there is a necessity to compare the sum variable in each loop to the previous sum - and replace if it's higher

snippsat thank you for the link !
Reply
#27
No lists at all.
elf, calories = 1, 0  # For keeping track of current elf
max_elf, max_calories = 1, 0  # For keeping track of elf with most calories
with open("test.txt", "r") as file:
    for line in file:
        if line != '\n':
            calories += int(line)
        else:
            # End of number group  Does this elf have the most calories?
            if calories > max_calories:
                max_elf, max_calories = elf, calories  # This remembers which elf has most calories
            # Set up to process the next number group
            elf, calories = elf+1, 0

# Have to process the last number group that is not followed by a  blank line
if calories > max_calories:
    max_elf, max_calories = elf, calories
print(max_elf, max_calories)
Reply
#28
ok, that's awesome

i had something like this in mind, like the general lines, i guess it got me confused a little

i'll try the website snippsat brought, it seems very cool, with missions/exercises that has a rising up level of difficulty

thank you !
Reply
#29
This was the next step. Compute the total for each group and print:
calories = 0
with open("test.txt", "r") as file:
    for line in file:
        if line != '\n':
            calories += int(line)
        else:
            print(calories)
            calories = 0
To get the max calories.
calories, max_calories = 0, 0
with open("test.txt", "r") as file:
    for line in file:
        if line != '\n':
            calories += int(line)
        else:
            max_calories = max(calories, max_calories)
            calories = 0
print(max_calories)
And then to remember the elf with the max calories add in elf and max_elf like in my previous example.
Reply
#30
that's awesome !

i'm not fond of being given the end result, so maybe (with the tunnel-vision i had) i had to just be given more hints (like...use an additional if condition)...and so on...

but i appreciate it
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020