Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Advent of Code
#1
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

Attached Files

.txt   input.txt (Size: 10.22 KB / Downloads: 92)
Reply
#2
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?
ndc85430 likes this post
Reply
#3
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
Reply
#4
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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
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.
Reply
#6
i need to create a sum of the chunks of numbers (chunks = the numbers between the separators).
Reply
#7
(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'))
Reply
#8
(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...
Reply
#9
(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...
Reply
#10
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.
Reply


Forum Jump:

User Panel Messages

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