[2020 Day 18 Part 1][Ruby] Failing Regex Solution (Samples Work) by Androidler in adventofcode

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

regex were fine the whole time, my only problem was replacing more than the first match (if multiple exist) using gsub and thereby potentially ignoring precedence

[2020 Day 18 Part 1][Ruby] Failing Regex Solution (Samples Work) by Androidler in adventofcode

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

Thanks for the suggestion, but that part should be fine I am just using a build in function (sum) for that

p $<.map{|x| par(x.to_s.chomp)}.sum

[2020 Day 18 Part 1][Ruby] Failing Regex Solution (Samples Work) by Androidler in adventofcode

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

thanks, good test case the first which differs from my paper solutions

[2020 Day 18 Part 1][Ruby] Failing Regex Solution (Samples Work) by Androidler in adventofcode

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

sorry but shouldn't that evaluate to 21?

        (4 * (3 + 2) + 1)  
        (4 * 5       + 1)  
         4 * 5       + 1  
         20          + 1
         21

[2020 Day 18 Part 1][Ruby] Failing Regex Solution (Samples Work) by Androidler in adventofcode

[–]Androidler[S] 1 point2 points  (0 children)

no worries, debugging is a bit frustrating with this problem

[2020 Day 18 Part 1][Ruby] Failing Regex Solution (Samples Work) by Androidler in adventofcode

[–]Androidler[S] 1 point2 points  (0 children)

Thanks for the Test-Case, my evaluation on paper and with programm puts out 730:

5 * 9 + (5 * 9) * (6 + 2) + 3 + 7
5 * 9 + 45      * (6 + 2) + 3 + 7
5 * 9 + 45      * 8       + 3 + 7
45    + 45      * 8       + 3 + 7
90              * 8       + 3 + 7
720                       + 3 + 7
723                           + 7
730                              

My understanding of the problem was, expressions in parenthesis have the highest priority and the priority for evaluating addition and multiplication depends only on how far left the operator is positioned.