Posts: 230
Threads: 39
Joined: Mar 2020
Dec-06-2022, 05:54 PM
(This post was last modified: Dec-06-2022, 06:19 PM by astral_travel.)
well, since break ends the loop, how do i restart it ? do i need to enclose all of it in a while loop ?
Posts: 6,780
Threads: 20
Joined: Feb 2020
Dec-06-2022, 06:27 PM
(This post was last modified: Dec-06-2022, 06:27 PM by deanhystad.)
Kind of a question that answers itself. Don't break.
You already have enough loops. no need for more.
Posts: 230
Threads: 39
Joined: Mar 2020
Dec-06-2022, 06:50 PM
(This post was last modified: Dec-06-2022, 07:05 PM by astral_travel.)
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\
Posts: 6,780
Threads: 20
Joined: Feb 2020
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.
Posts: 7,313
Threads: 123
Joined: Sep 2016
Dec-06-2022, 07:50 PM
(This post was last modified: Dec-06-2022, 07:50 PM by snippsat.)
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.
Posts: 230
Threads: 39
Joined: Mar 2020
Dec-06-2022, 08:41 PM
(This post was last modified: Dec-06-2022, 08:41 PM by astral_travel.)
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 !
Posts: 6,780
Threads: 20
Joined: Feb 2020
Dec-06-2022, 09:19 PM
(This post was last modified: Dec-06-2022, 09:20 PM by deanhystad.)
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)
Posts: 230
Threads: 39
Joined: Mar 2020
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 !
Posts: 6,780
Threads: 20
Joined: Feb 2020
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.
Posts: 230
Threads: 39
Joined: Mar 2020
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
|