Python Forum

Full Version: Advent of Code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
okay, another try (that doesn't work):

with open("/home/tal/PycharmProjects/pythonProject2/input.txt", "r") as file:


    for line in file:

        line_total = 0
        if line != "\n":
            line_total += int(line)
        else:
            print(line_total)
you know, when i read code of other people - it makes perfect sense to me, but when i try to write a code of my own - it seems like i just don't know how to do it...
(Dec-05-2022, 09:06 PM)astral_travel Wrote: [ -> ]okay, another try (that doesn't work):
Now is worse Wink
Use deanhystad code in last post with some modification.
sum_tot = []
with open("test.txt", "r") as file:
    sum_list = []
    for line in file:
        if line != "\n":
            sum_list.append(int(line))
        else:
            sum_tot.append(sum_list)
            sum_list = [] 
>>> sum_tot
[[1000, 2000, 3000], [4000], [5000, 6000], [7000, 8000, 9000]]
So now you see that it grouped need this because have sum up each group(Elf's).
sum_tot.append(sum(sum_list))
>>> sum_tot
[6000, 4000, 11000, 24000]
No almost there.
I don't think it is worse. I think that is actually pretty close. The problem is where you reset the total. You are doing this:
Output:
numbers = 1 2 3 4 5 6 total = 0 total = total + 1 total = 0 total = total + 2 total = 0 total = total + 3 total = 0 total = 0 total = total + 4 . . .
When should you reset the total?
(Dec-05-2022, 09:50 PM)deanhystad Wrote: [ -> ]When should you reset the total?
I think is hard to help if you not read the task at Advent of Code.
Need max bye grouped calculation,so if add this should be ok.
>>> sum_tot
[6000, 4000, 11000, 24000]
>>> max(sum_tot)
24000   
okay i did like this:

with open("/home/tal/PycharmProjects/pythonProject2/input.txt", "r") as file:

    line_total = 0

    for line in file:

        if line != "\n":
            line_total += int(line)
        else:
            print(line_total)
at least it gave some numbers...

how can i reset it properly ?

i think i'm too tired now to understand anything, it's 00:07 here, and i really tried today to advance...

snippsat - i have read the task before i started, and understood it
@astral_travel: That will work for one group. How do you get ready for the next group? It might help to replace the Python code with pseudo-code.
start_group  # Can be inside or outside "with open . . ."
with open(calorie_file, "r") as file:
    for line in file:
        if line != "\n":
           add_number_to_total
        else:
            process_totall
You have the "add_number_to_total" all figured out. Now you just have to figure out what you want to do when you've read all the numbers in the group. You don't need to print the total. That is fine for debugging but doesn't do anything for solving the problem. For the next step you should modify your code to keep track of the largest total. That means process_total will expand to:
save_max_total
start_group
Snippsat is talking to me about reading the challenge, I think. I did pull up the advent challenges and I think this is the elf/calories challenge. There is a need to compute a total for each grouping of numbers, but that does not require having a list. What hasn't been addressed yet is keeping track of what elf has the most calories. This could be done using a list, but it can also be done without any lists.
okay,
i need to understand something for a moment,
in your last message, in the code, in line 4: if line != "\n":

the way i understand it - it means - if line is not equal to \n - which means it is not a separator - which means it is a number - then add it...

have i understood it correctly ?

@snippsat
what you did is quite impressive,
the thing is, if i don't use a list to save the data - the way i understand it - i have to compare the total of each round with its predecessor in a variable and eventually have the highest total prevail (in a way).....am i correct ?
\n is not a separator. \n is a linefeed or newline character. Every line in the file except the last has a \n tacked on at the end. The separator for this program is a blank line. When the file has a blank line the string is "\n" (blank followed linefeed).

Why are you asking now? This is exactly what you've been doing from the very start. If the line is not blank, convert line to number and do something with the result. Originally you were appending to a long list. Now you are adding to a total.
if i do something like this:

with open("/home/tal/PycharmProjects/pythonProject2/input.txt", "r") as file:

    line_total = 0

    for line in file:

        if line != "\n":
            line_total += int(line)
        else:
            break

        print(line_total)
it gives me the total of the first group:

Output:
5794 11673 16572 23349 29194 30497 37258 39072 45677 50392 52656 55445
and if i put the print(line total) at the start of the line - it gives me just the end result of that group:

with open("/home/tal/PycharmProjects/pythonProject2/input.txt", "r") as file:

    line_total = 0

    for line in file:

        if line != "\n":
            line_total += int(line)
        else:
            break

print(line_total)
Output:
55445
Great! Can you modify the code to print the total for each group. I think there are 249 groups.
Pages: 1 2 3