Skip to content

Instantly share code, notes, and snippets.

@sudhackar
Created November 16, 2021 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudhackar/882e95dc79cfcb4b384b2f919b2d7ab7 to your computer and use it in GitHub Desktop.
Save sudhackar/882e95dc79cfcb4b384b2f919b2d7ab7 to your computer and use it in GitHub Desktop.
csaw maze euler knight walk solution
from functools import reduce
# copied closed knight walk from https://mathworld.wolfram.com/images/eps-gif/KnightsTours_700.gif
x = [[50, 45, 62, 41, 60, 39, 54, 35],
[63, 42, 51, 48, 53, 36, 57, 38],
[46, 49, 44, 61, 40, 59, 34, 55],
[43, 64, 47, 52, 33, 56, 37, 58],
[26, 5, 24, 1, 20, 15, 32, 11],
[23, 2, 27, 8, 29, 12, 17, 14],
[6, 25, 4, 21, 16, 19, 10, 31],
[3, 22, 7, 28, 9, 30, 13, 18]][::-1]
assert(True == reduce(lambda x, y: x and y, [
reduce(lambda x, y: x or y, [j in i for i in x]) for j in range(1, 65)]))
mp = [(-2, 1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2), (-1, -2), (-2, -1)]
l = [(0, 0) for i in range(65)]
for i, row in enumerate(x):
for j, v in enumerate(row):
l[v] = (i, j)
_i = 51
while True:
i = _i + 1
if i == 51:
break
for j, move in enumerate(mp):
nextx, nexty = l[i][0]+move[0], l[i][1]+move[1]
if nextx >= 0 and nexty >= 0 and nextx < 8 and nexty < 8 and x[nextx][nexty] == 1+(i%64):
print(j+1,end="")
_i = (_i+1) % 64
#include "pin.H"
#include <stdio.h>
#include <fstream>
#include <iostream>
using namespace std;
PIN_LOCK globalLock;
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "pin.out", "specify output file name");
ofstream outFile;
ADDRINT l, h;
VOID callback_image(IMG img, VOID *v)
{
if (IMG_IsMainExecutable(img))
{
l = IMG_LowAddress(img);
h = IMG_HighAddress(img);
}
}
VOID logme(ADDRINT ip, CONTEXT *ctx)
{
PIN_REGISTER regval;
PIN_GetContextRegval(ctx, REG_RAX, reinterpret_cast<UINT8 *>(&regval));
outFile << std::hex << regval.dword[0] << ":";
int idx = (regval.dword[0] - 0x4015DF) / 205;
PIN_GetContextRegval(ctx, REG_RDI, reinterpret_cast<UINT8 *>(&regval));
ADDRINT value;
ADDRINT *last_char = (ADDRINT *)(regval.qword[0]-1);
PIN_SafeCopy(&value, last_char, sizeof(char));
PIN_GetContextRegval(ctx, REG_R15, reinterpret_cast<UINT8 *>(&regval));
outFile << char(value) << ":" << std::dec << 1+((idx/12)*8+(idx%12)) << ":" << regval.dword[0] << endl;
}
VOID callback_instruction(INS ins, VOID *v)
{
if (INS_Opcode(ins) == XED_ICLASS_JMP && INS_OperandReg(ins, 0) == REG_RAX)
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)logme, IARG_INST_PTR, IARG_CONTEXT,
IARG_END);
}
}
VOID fini(INT32 code, VOID *v)
{
outFile.close();
}
int main(int argc, char *argv[])
{
if (PIN_Init(argc, argv))
{
perror("init");
return 0;
}
outFile.open(KnobOutputFile.Value().c_str());
IMG_AddInstrumentFunction(callback_image, 0);
INS_AddInstrumentFunction(callback_instruction, 0);
PIN_AddFiniFunction(fini, 0);
PIN_StartProgram();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment