alittle different version but using c++ instead of python