Python Forum
Advent of Code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Advent of Code (/thread-38874.html)

Pages: 1 2 3


Advent of Code - astral_travel - Dec-05-2022

hey,
i'm trying the advent of code challange,

i currently have the following code:

input_file = open("/home/tal/PycharmProjects/pythonProject2/input")

data = input_file.readlines()

newList = []

for line in data:
    if line != "\n":
        newList.append(int(line.rstrip('\n')))
    else:
        newList.append("")

print(newList)
print(type(newList[2]))
i attached the input file

i don't know how to sum the chunks of numbers, like, between the '' separators,
any advice ?

thanks


RE: Advent of Code - deanhystad - Dec-05-2022

Why are you asking for help solving a challenge? Isn't solving it yourself the "challenging" part? Otherwise, what is the point?

Why are you putting '' in as a separator?


RE: Advent of Code - astral_travel - Dec-05-2022

yea but i'm trying and trying and searching for resources on this on the internet and nothing comes up and i'm not finding anything and am lacking the basic knowledge to somehow figure this out....i simply don't know how to add up the numbers up to/between the separators....

what can i do


RE: Advent of Code - rob101 - Dec-05-2022

You're almost there.

All you need to do now, is to loop through the list of int values, adding each one to a total, until you hit your separators, then append the total to a new list, reset the total to zero, and go again. Once that's done, you can use the max() function, on the totals list, to get the max value.

Hint:
for number in newList:
    print(number)
That's how I would do it, but I'm sure there's more than one way.


RE: Advent of Code - deanhystad - Dec-05-2022

Hint, get rid of new_list.

Let's say you are a cashier at the grocery store. There are 5 customers in your line. Do you make customers wait as you scan all 5 carts, or do you scan the first cart, computing the total as you scan, and take the customer's money before moving on to the next cart?

Most coding challenges don't have much to do with coding. They are puzzles that test your problem-solving skills. You don't need to know any fancy Python to solve this challenge. Think about how you would solve with pencil and paper, and I guarantee there will be no lists with separators and no wondering how to take the sum of a list. Solve first, then code.


RE: Advent of Code - astral_travel - Dec-05-2022

i need to create a sum of the chunks of numbers (chunks = the numbers between the separators).


RE: Advent of Code - deanhystad - Dec-05-2022

(Dec-05-2022, 07:53 PM)astral_travel Wrote: i need to create a sum of the chunks of numbers (chunks = the numbers between the separators).
Yes, you need to calculate sums. Do you need a list for that?

Also, there is no need for the rstrip() here. int() doesn't care if "\n" following the digits.
int(line.rstrip('\n'))



RE: Advent of Code - astral_travel - Dec-05-2022

(Dec-05-2022, 07:31 PM)deanhystad Wrote: Let's say you are a cashier at the grocery store. There are 5 customers in your line. Do you scan all 5 carts and then compute the totals, making the poor customers wait before taking their money? Or do you scan the first cart, computing the total as you scan? When the cart is empty you know the total for the first customer and are ready to take their money.

Most coding challenges don't have much to do with coding. They are puzzles that test your problem-solving skills. You don't need to know any fancy Python to solve this challenge. Think about how you would solve with pencil and paper, and I guarantee there will be no lists with separators and no wondering how to take the sum of a list. Solve first, then code.

the logic is clear to me, i just don't know how to tell the program to sum it up until it hits a separator/delimiter.

i thought of something like this:

input_file = open("/home/tal/PycharmProjects/pythonProject2/input.txt")

data = input_file.readlines()

newList = []

for line in data:
    if line != "\n":
        newList.append(int(line.rstrip('\n')))
    else:
        newList.append("")

print(newList)
print(type(newList[2]))

sum_list = []

for i in newList:
    sum_list.append(i)

for x in sum_list:
    sum_list = sum(sum_list)
    if x == "":
        pass
    print(sum_list)
idk....feeling stuck...


RE: Advent of Code - astral_travel - Dec-05-2022

(Dec-05-2022, 08:08 PM)deanhystad Wrote:
(Dec-05-2022, 07:53 PM)astral_travel Wrote: i need to create a sum of the chunks of numbers (chunks = the numbers between the separators).
And why do you need a list for that?

okay, you're right, i don't...
but i just don't know how to sum the chunks...i wrote a lil more code in the message above..

if it's one long array of numbers you simply use sum() and that's it....but sum() doesn't work when you're trying to add up the numbers up to the delimiter...


RE: Advent of Code - deanhystad - Dec-05-2022

Your latest attempt can be compressed to a single loop,
with open("test.txt", "r") as file:
    sum_list = []
    for line in file:
        if line != "\n":
            sum_list.append(int(line))
        else:
            print(sum_list)
            sum_list = []
Now get rid of sum_list because we don't need the list. We need the total.