First  Prev  1  2  3  Next  Last
Post Reply nth binary permutation for any binary numbers
3143 cr points
Send Message: Send PM GB Post
30 / M
Offline
Posted 10/16/16 , edited 10/16/16
Well I know you can't go backward now, but if you can find a pattern on how it goes downward 8,7,6,5,4,3,2,1, then you can still find the previous number. For one the position is always decrementing, I stored the total number of bit count, both zeros and ones in there. If you can find a pattern on position then you might be able to back solve for k. Not that I'm seeing one though

P.S. Maybe apart from ranking we derive another function for getting the number of bit counts for each rank. 8=4, 7=3, 6=3, 5=3, 4=3, 3=2, 2=2, 1=1. If Kayaba does it too fast then we're doomed :D

P.S. Well but predicting position and predicting N is pretty much the same thing
3143 cr points
Send Message: Send PM GB Post
30 / M
Offline
Posted 10/18/16
Good job beating the game ^^!
3143 cr points
Send Message: Send PM GB Post
30 / M
Offline
Posted 10/28/16
Must upload before computer breaks

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Web;


namespace Dino.Tools.WebMonitor
{
public class FakeChartForm1 : Form
{
private System.ComponentModel.IContainer components = null;
System.Windows.Forms.DataVisualization.Charting.Chart chart1;


static System.Array colorsArray = Enum.GetValues(typeof(KnownColor));
KnownColor[] allColors = new KnownColor[colorsArray.Length];



public static byte[] bytes = System.IO.File.ReadAllBytes(@"E:\Data\small_test.gif");

public static byte[] bytes_new;

public FakeChartForm1()
{
InitializeComponent();



}

private double f(int i)
{
var f1 = 59894 - (8128 * i) + (262 * i * i) - (1.6 * i * i * i);
return f1;
}

private void Form1_Load(object sender, EventArgs e)
{
Array.Copy(colorsArray, allColors, colorsArray.Length);

chart1.Series.Clear();

StringBuilder sb = new StringBuilder();


Series[] series1 = new System.Windows.Forms.DataVisualization.Charting.Series[30];

for (int i = 1; i < 50; i++)
{
sb.Clear();
sb.Length = 0;
sb.Capacity = 0;

sb.Append(Convert.ToString(i, 2));
//.Append("100010");

chart1.Series.Add("Series1" + i.ToString());

chart1.Series["Series1" + i.ToString()].Color = Color.FromName(allColors[i%100+50].ToString());
chart1.Series["Series1" + i.ToString()].IsVisibleInLegend = true;
chart1.Series["Series1" + i.ToString()].IsXValueIndexed = false;
chart1.Series["Series1" + i.ToString()].ChartType = SeriesChartType.Line;

chart1.Series["Series1" + i.ToString()].Points.AddXY(0, i);

callmain(sb, "Series1" + i.ToString());
}


//sb.Append("1000100010101010101010010111111");
//callmain(sb);

chart1.Invalidate();
}

public void callmain(StringBuilder sb, String series1)
{






int star = 0;


/*
for (int k = 0; k < bytes.Length; k++)
{
sb.Append(Convert.ToString(bytes[k], 2));
}

sb.Clear();
sb.Append("10101011");
*/


/*
BigInteger nb = new BigInteger(bytes.Concat(new byte[] { 0 }).ToArray());

bytes_new = nb.ToByteArray();


for (int i = 0; i < bytes_new.Length; i++)
{
sb.Append(Convert.ToString(bytes_new, 2).PadLeft(8, '0'));
}
*/





//sb.Append("1000100010101010101010010111111");


Console.WriteLine(sb);
Console.WriteLine();



int[] one = new int[100];
int[] zero = new int[100];
int[] position = new int[100];

int[] new_one = new int[2000];
int[] new_zero = new int[2000];
int[] new_position = new int[2000];

int[] n = new int[100];
int[] coun = new int[100];


for (int i = 0; i < 100; i++)
{

for (int j = 0; j < sb.Length; j++)
{
char c = sb[j];
if (c == '0')
{
zero++;
}
else
{
one++;
}
}


//one = IteratedBitcount(number_compress);
//zero = IteratedZerocount(number_compress);
position = one + zero;

BigInteger fac = Factorial(one);



n = (int)FindN(sb, one, position, fac);

chart1.Series[series1].Points.AddXY(i+1, n);







sb = ToB(n, sb);

Console.WriteLine(sb);
Console.WriteLine(position);
Console.WriteLine(one);
Console.WriteLine(zero);
//Console.WriteLine(n);
Console.WriteLine();



//sb.Append(string.Join("",n.ToByteArray().Select(x => Convert.ToString(x, 2).PadLeft(8, '0'))));



if (sb.ToString() == "" || sb.ToString() == "0" || sb.ToString() == "1" || sb.ToString() == "11" || sb.ToString() == "111" || sb.ToString() == "1111" || sb.ToString() == "11111" || sb.ToString() == "111111")
{
if (sb.ToString() != "")
{
chart1.Series[series1].Points.AddXY(i + 2, 0);
}
star = i;
break;
}


}

//Console.WriteLine(number_compress);
//Console.WriteLine();

/*
for (int i = 0; i < star; i++)
{
new_one = one - one[i + 1];
new_zero = zero - zero[i + 1];
new_position = position - position[i + 1];
}


BigInteger number_decompress = 0;

for (int i = star; i >= 0; i--)
{

BigInteger fac = Factorial(one);

number_decompress = FindNumber(sb, one, position, fac);

sb = ToB(number_decompress, sb);

//sb.Append(string.Join("",number_decompress.ToByteArray().Select(x => Convert.ToString(x, 2).PadLeft(8, '0'))));
Console.WriteLine(sb);
Console.WriteLine();
}

//Console.WriteLine(number_decompress);
Console.WriteLine();



byte[] final = new byte[sb.Length / 8];

for (int i = sb.Length / 8 - 1; i >= 0; i--)
{
final = Convert.ToByte(sb.ToString(i * 8, 8), 2);
}

BigInteger b = new BigInteger(final);

bytes_new = b.ToByteArray();

//byte[] buffer = Encoding.ASCII.GetBytes(sb.ToString());

File.WriteAllBytes(@"E:\Data\new_Blank.gif", bytes_new);
*/
}

static StringBuilder ToB(BigInteger n, StringBuilder sb)
{
sb.Length = 0;
sb.Capacity = 0;
sb.Clear();

int num = 0;
while (BigInteger.Pow(2, num) <= n)
{
num++;
}

for (int k = num - 1; k >= 0; k--)
{
BigInteger counter = BigInteger.Pow(2, k);
if (n >= counter)
{
sb.Append("1");
n = n - counter;
}
else
{
sb.Append("0");
}

}

return sb;
}

static string ToBinaryString(BigInteger bigint)
{
var bytes = bigint.ToByteArray();
var idx = bytes.Length - 1;

// Create a StringBuilder having appropriate capacity.
var base2 = new StringBuilder(bytes.Length * 8);

// Convert first byte to binary.
var binary = Convert.ToString(bytes[idx], 2);



// Ensure leading zero exists if value is positive.
if (binary[0] != '0' && bigint.Sign == 1)
{
base2.Append('0');
}


// Append binary string to StringBuilder.
base2.Append(binary);

// Convert remaining bytes adding leading zeros.
for (idx--; idx >= 0; idx--)
{
base2.Append(Convert.ToString(bytes[idx], 2).PadLeft(8, '0'));
}

return base2.ToString();
}

static BigInteger FindNumber(StringBuilder sb, int one, int position, BigInteger fac)
{
BigInteger number = 0;
BigInteger n = 0;
int k = one;

for (int l = 0; l < sb.Length; l++)
{
if (sb[l] == '1')
{
n = n + BigInteger.Pow(2, sb.Length - l - 1);
}
}

for (int j = position - 1; j >= 0; j--)
{
BigInteger mult = j;
BigInteger num = 0;

for (int i = 1; i < (int)k; i++)
{
mult *= (j - i);
}


num = (mult / fac);



if (num <= n)
{
number = number + BigInteger.Pow(2, j);
fac /= k;
k--;
n = n - num;
}
//Console.WriteLine(k);
if (k == 0)
break;
}
return number;
}

static BigInteger FindN(StringBuilder sb, int one, int position, BigInteger fac)
{
/*
BigInteger findMSB = number;
BigInteger test = number;
BigInteger MSB = 1;
while (findMSB > 0)
{
MSB <<= 1;
findMSB >>= 1;
}
MSB >>= 1;
*/

BigInteger n = 0;
int k = one;
int pos = position;

for (int i = 0; i < sb.Length; i++)
{
if (sb == '1')
{
int a = pos - 1;
BigInteger mult = a;

for (int j = 1; j < k; j++)
{
mult *= (BigInteger)(a - j);
}

n += (mult / fac);

fac /= k;
k--;

}
//Console.WriteLine(k);
if (k == 0)
break;
pos--;

}
return n;
}

static int IteratedBitcount(BigInteger n)
{
BigInteger test = n;
int count = 0;

while (test != 0)
{
if ((test & 1) == 1)
{
count++;
}
test >>= 1;
}
return count;
}

static int IteratedZerocount(BigInteger n)
{
BigInteger test = n;
int count = 0;

while (test != 0)
{
if ((test & 1) == 0)
{
count++;
}
test >>= 1;
}
return count;
}

static BigInteger Factorial(int arg)
{
BigInteger value = 1;
for (int index = 2; index <= arg; index++)
{
value *= index;
}
return value;
}

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit();
this.SuspendLayout();
//
// chart1
//
chartArea1.Name = "ChartArea1";
this.chart1.ChartAreas.Add(chartArea1);
this.chart1.Dock = System.Windows.Forms.DockStyle.Fill;
legend1.Name = "Legend1";
this.chart1.Legends.Add(legend1);
this.chart1.Location = new System.Drawing.Point(0, 50);
this.chart1.Name = "chart1";
// this.chart1.Size = new System.Drawing.Size(284, 212);
this.chart1.TabIndex = 0;
this.chart1.Text = "chart1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.chart1);
this.Name = "Form1";
this.Text = "FakeChart";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();
this.ResumeLayout(false);
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FakeChartForm1());
}
}
}
20904 cr points
Send Message: Send PM GB Post
22 / M
Online
Posted 10/28/16
First  Prev  1  2  3  Next  Last
You must be logged in to post.