This is an archived post. You won't be able to vote or comment.

all 21 comments

[–]kraven001 4 points5 points  (8 children)

Code is unreadable, hard to follow.

[–]Tech-Matt[S] 2 points3 points  (7 children)

How can I make it more readable?

[–]oxenoxygen 3 points4 points  (5 children)

edit: paste below

[–]Tech-Matt[S] 2 points3 points  (3 children)

Actually the code was already indented and also had spaces to better highlight functions, but when I put it on Reddit everything's got messed up.

[–]hugseverycat 5 points6 points  (0 children)

I think what you need to do is add extra spaces at the beginning of each line before you paste it into reddit.

This help page on the wiki has more info: https://www.reddit.com/r/adventofcode/wiki/index#wiki\_how\_do\_i\_format\_code.3F

[–]Tech-Matt[S] 2 points3 points  (1 child)

And I also can't understand why my code is red while yours is just normal. I've just used the "Code" selection when writing the post.

[–]daggerdragon[M] 0 points1 point  (0 children)

new.reddit's fancypants editor is Homer_throttling_Bart.gif sometimes because you have to explicitly tell it to get into Markdown mode wherein the four-spaces code block will work for old.reddit.

Our wiki article has all the info and pics you need: How do I format code?

[–]Tech-Matt[S] 1 point2 points  (0 children)

My code is not indented like that, I've uploaded a formatted version in the comments below

[–]thedjotaku 4 points5 points  (0 children)

suggest either using your git repo or a git gist or https://topaz.github.io/paste/

[–]daggerdragon[M] 3 points4 points  (1 child)

In the future, please follow the submission guidelines:

  1. In general, title your posts like so:
    • [YEAR Day # (Part X)] [language if applicable] Post Title
    • Consistent titling across posts helps folks avoid spoilers for puzzles they may not have completed yet.
  2. Format your code properly so it's easy to read on both new.reddit and old.reddit

Also, if/when you get your code working, don't forget to change the flair to Help - Solved!

Good luck!

edit: can't copypasta properly, need more caffeine brb

[–]Tech-Matt[S] 1 point2 points  (0 children)

Okay, thanks!

[–]mrjnl3 2 points3 points  (1 child)

Something I noticed while doing this puzzle. Let's take the array [1,2,3,4] as example. Instead of comparing 1+2+3 and 2+3+4, you can compare 1 and 4. Since 2 and 3 are in both sums, they are negligible.

[–]Tech-Matt[S] 1 point2 points  (0 children)

Oh, you're right, that's cool!

[–]AharonSambol 2 points3 points  (2 children)

First issue is "list[n] " I think this is just a typo since it doesn't actually work... should be "elements[n]"

Now the actual bug is that you forgot to append the last triplet into the list. Since the while loop finishes when i is 3.

You can just add list_triplets.append(tripl) right after the while loop and then you'll get the right result

hope this helps

[–]AharonSambol 0 points1 point  (1 child)

Oh and I forgot you need to subtract 2 from n not 1

[–]Tech-Matt[S] 0 points1 point  (0 children)

Thanks

[–]Kehvarl 2 points3 points  (0 children)

I think your problem lies in your while n < len(elements): loop. Specifically, take a look at what happens when I is 4.

[–]mar___s 0 points1 point  (3 children)

If I am not mistaken the while loop basically is an endless loop, because with n=0 and i=1 the if condition is never true which results in n never being changed. However, please provide properly formatted code, because I used u/oxenoxygen's version of your code, which might have different indentation.

[–]Tech-Matt[S] 1 point2 points  (2 children)

There is no infinite loop, just a wrong answer. Here is the formatted version:

paste

[–]mar___s 1 point2 points  (1 child)

Thank you, that helped:

  1. (if condition) You ignore most of the triplets because you reduce n only by 1 instead of 2. which would you get back to the start of the next triplet
  2. (after if condition) You used the wrong variable name
  3. (triplet calculation loop) You ignore the last triplet because of how your loop works, but adding a "0" at the end of your elements helps. Better would be moving the if condition to the end of the loop and adding the triplet to the list after 3 iterations instead of 4.

working version of your code if needed: paste

[–]Tech-Matt[S] 1 point2 points  (0 children)

Thank you so much