Skip to content

Instantly share code, notes, and snippets.

@oupo
Created March 31, 2021 16:28
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 oupo/5afe1d94fad515059d624229942e81a9 to your computer and use it in GitHub Desktop.
Save oupo/5afe1d94fad515059d624229942e81a9 to your computer and use it in GitHub Desktop.
using System;
namespace shake_balls
{
class ShakeBalls
{
static void Main(string[] args)
{
int[] search = { 1, 4, 2, 4, 1, 2, 4, 1, 2, 4, 2, 1, 4, 1, 2, 4, 3, 4, 1, 2, 4, 3, 4, 2, 1, 4, 2, 1, 4, 2, 1, 2, 4, 1 };
for (UInt64 s = 0; s < (1 << 19); s++)
{
LCG lcg = new LCG((uint)s);
BallSimulator simulator = new BallSimulator(lcg, 3);
if (simulator.Match(search))
{
Console.WriteLine("{0:x8}", (uint)s);
}
}
Console.WriteLine("Finish");
}
}
class LCG
{
public uint seed { get; set; }
public LCG(uint seed)
{
this.seed = seed;
}
public int rand()
{
seed = seed * 0x41c64e6d + 0x6073;
return (int)(seed >> 16);
}
}
class BallSimulator
{
private LCG lcg;
private int num;
private int[] counters;
private int frame;
public BallSimulator(LCG lcg_, int num_)
{
lcg = lcg_;
num = num_;
counters = new int[num];
frame = 0;
}
int SimulateOne()
{
lcg.rand();
int shaked = 0;
for (int i = 0; i < num; i++)
{
if (counters[i] == 0)
{
int c = 10 + lcg.rand() % 8;
counters[i] = 13 + c;
shaked |= (1 << i);
}
else
{
counters[i]--;
}
}
if (frame == 0) lcg.rand();
frame++;
return frame == 1 ? 0 : shaked;
}
public bool Match(int[] search)
{
int index = 0;
while (index < search.Length)
{
int shaked = SimulateOne();
if (shaked != 0)
{
if (search[index] != shaked) return false;
index++;
}
}
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment