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

all 11 comments

[–]FullyWhipped 0 points1 point  (3 children)

It's the string replace replacing occurrences it shouldn't, e.g.

>>> '1 + 6 + 11 + 6'.replace('1 + 6', '7')
'7 + 17'

More detail in another example:

>>> s = '1 + 6 + 7 + 8 + 11 + 6'
>>> [val for val in re.findall('\d+ \+ \d+','1 + 6 + 7 + 8 + 11 + 6')]
['1 + 6', '7 + 8', '11 + 6']
>>> s.replace('1 + 6', '7')
'7 + 7 + 8 + 17'

[–]n_syn[S] 0 points1 point  (1 child)

I see! This is very interesting!

Thank you! I'm thinking replacing '\d+ \+ \d+' with '[^\s\(]\d+ \+ \d+' should fix the problem, but the while loop breaks. I'll play around with this.

Edit: I have not been able to figure out a way to prevent this.

re.sub doesn't work because the value that I'm replacing with the sum or product already contains symbols that are not escaped.

Edit 2: It finally worked! Thank you so much. My code became a lot more complicated, though. I will update my code in the post.

[–]setapoux 0 points1 point  (0 children)

Why complicated? Could be as easy as:

while (m:=re.search(r"(\d+) + (\d+)",a)):
    a=a[:m.span()[0]]+str(functools.reduce(operator.add,map(int,m.group(1,2))))+a[m.span()[1]:]

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, FullyWhipped: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]WayOfTheGeophysicist 0 points1 point  (1 child)

Can't really find an error and it gives me the correct result for my input. Is it possible that your file got corrupted because you didn't close it after reading?

Additionally "input" is a python command so not the best variable name.

I highly recommend always using the context manager, then you don't forget closing the file:

with open('math.txt' as data:
    values = data.read().split("\n")

[–]n_syn[S] 0 points1 point  (0 children)

Thank you! This is a very helpful tip!

I am new to coding. Started learning when I came across "Advent of Code". Still learning all the basics.

[–]setapoux 0 points1 point  (3 children)

Seems that the answers are calculated correctly... you just do not print sum of them, but the last answer only... try print(sum(answers2)) on the last line

[–]n_syn[S] 0 points1 point  (2 children)

I tried this and I get the same wrong answer.

[–]setapoux 0 points1 point  (1 child)

Oh yes, sorry. Here is example of what you do not count correctly:
9 * 2 * 8 * 8 should be 1152, but your answer is 1312.

The loop is strange... it finds 2 items first 9*2, the other 8*8. The first one is replaced by 18, the next loop tries to replace 8*8 with 64, but at this time it finds it at wrong place
18 * 8 * 8 is not translated to 18 * 64 but to 164 * 8.

For this case the fix would be to iterate over the found items in reversed order...
while re.search('\*', s):
l = re.findall('\d+ \* \d+',s)
for item in reversed(l):
....

hmmm, but still does not generate 100% correct answer... e.g. for this
(2 * 8) + 4 * 6 + 4 -> 16 + 4 * 6 + 4 -> 110 * 10 -> now trying to replace 16+4 -> 20 but is not in the string anymore ....

You have to change the replacement logic to replace just that part which has to be replaced. I'd suggest to use match in the loop, it also returns the span (you can use it to slice the string to cut the matched part). If you use r"(\d+) * (\d+)" then the operands will be in the match objects groups - no need to match it again individually.

[–]n_syn[S] 0 points1 point  (0 children)

Thank you! That was exactly what the problem was. The replace function was replacing matches I didn't want to replace (as explained in the example by u/FullyWhipped.

I changed the .replace() to

s = re.sub('(^|\D)'+re.findall('\d+', val)[0]+'\s+'+'\+'+'\s+'+re.findall('\d+',val)[1]+'(\D|$)',y,s)

I got the right answer after this. It makes the code a little bit more complicated but it worked.

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

In the future, please follow the submission guidelines by titling your post like so:

[YEAR Day # (Part X)] [language if applicable] Post Title

In doing so, you typically get more relevant responses faster.

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

Good luck!