# not-really-math (346 solves, 319 points)
## Description
Have a warmup algo challenge.

``nc not-really-math.hsc.tf 1337``


## Solution
My first instinct was to create a script that would find the index of all the addition operations (The a's). It would then find the values on either side of the operation
and add them. It would do this for every a in the problem then multiply the remaining numbers. This works great for the test case in the pdf and other problems I could think of. This algorithm breaks down when addition opperations are chained like below:


My flawed algorithm would compute ``4 + 3``, then ``3 + 6``, and then ``6 + 2``. It always will repeat values when adding. After fiddling with ways to detect addition chaining and forming exceptions, the code got too complicated to fast. I had to rethink my approach.

I thought of a new method that would pull all groups of addition out as a substring. The algorithm would replace the a's with a "+" and use python's ``eval()`` function to compute the results. Results were stored in a list and later multiplied out. The final result was modded by 2<sup>32</sup> - 1 and sent to the nc server with pwntools.

Final script is below:

import re
from pwn import *

def multiplyList(list):
total = 1
if not list:
return 1
for num in list:
total *= num
return total

def find_char(char):
match = char
match_indexes = []
for index, character in enumerate(problem):
if character == match:
return match_indexes

def getAddition():
index = 0
addition = []
phrase = ""
mLocation = find_char("m")
if not mLocation:
return -1
for character in problem:
if character == "m":
phrase += character
while index < len(mLocation):
phrase = ""
start = mLocation[index] + 1
end = mLocation[index + 1]
except IndexError:
for i in range(start, len(problem)):
phrase += problem[i]
for i in range(start, end):
phrase += problem[i]
index += 1
return addition

io = process(['nc', 'not-really-math.hsc.tf', '1337'])
mod = 2**32 - 1
while True:
data = io.recvuntil(":").decode()
except EOFError:
problem = re.search("([\dam]{4,})\n", data).group(0)
print("[RECIEVE]: " + problem)
final = 0
addition = getAddition()
if addition == -1:
problem = problem.replace("a", "+")
final = eval(problem)
addition = [phrase.replace("a", "+") for phrase in addition]
values = []
edit = problem
for phrase in addition:
edit = edit.replace(phrase.replace("+", "a"), "", 1)
edit = edit.replace("m", " ")
mult = edit.split()
#print(mult, values)
mult = [int(i) for i in mult]
final = multiplyList(mult) * multiplyList(values)
print("[SEND]: " + str(final % mod))
io.sendline(str(final % mod))

Running the code gives the flag!
## Flag

Original writeup (https://github.com/BASHing-thru-challenges/HSCTF-2021-Writeups/tree/main/algo/not-really-math).