Skip to content

Instantly share code, notes, and snippets.

@Chirimen-Jako
Last active September 25, 2019 12:30
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 Chirimen-Jako/3a73e2e952ce0cc126e382455e341f63 to your computer and use it in GitHub Desktop.
Save Chirimen-Jako/3a73e2e952ce0cc126e382455e341f63 to your computer and use it in GitHub Desktop.
おっぱいそんちくびんびん♪ (Arduino)
/*
* h_sketch_sandwich_oppai.ino
*
* 2019/08/31: initial release
*
* Arduino IDE 1.8.9
*
* Arduino UNO (or compatible)
* Arduino Leonardo (or compatible)
* Arduino nano (or compatible)
* Arduino micro (or compatible)
* Arduino pro micro (or compatible)
* Arduino pro mini (or compatible)
*
* Dedicated to shirasu
* https://gist.github.com/8q/a5331c6ef8a205b32125
*/
void swap(byte & a, byte & b) {
byte tmp = a;
a = b;
b = tmp;
}
void shuffle_table(byte table[], int x, int i, const int C) {
byte b = table[byte(((x ^ i) + millis()) % C)];
byte n1 = byte((b + 1) / 2);
for (byte j = 0; j < n1; j++) {
swap(table[j], table[b - j - 1]);
}
byte n2 = byte((C - b) / 2);
for (byte j = 0; j < n2; j++) {
swap(table[b + j], table[C - j - 1]);
}
}
long createSeed() {
Serial.println("乱数の初期値を計算します。");
Serial.println("任意のASCII文字を最低8文字以上入力して、 [Enter] を押してください。");
String strSeed;
Serial.setTimeout(-1);
while (strSeed.length() < 8) {
String line_in = Serial.readStringUntil('\n');
for (int i = 0; i < line_in.length(); i++) {
char c = line_in[i];
if (isGraph(c)) {
strSeed.concat(c);
}
}
if (strSeed.length() < 8) {
Serial.println(String("あと、") + (8 - strSeed.length()) + "文字入力してください。");
}
}
unsigned long seed = millis();
unsigned long seed_temp = micros() >> 3;
const unsigned long C = ('~' + 1) - '!';
byte table[C];
for (int i = 0; i < C; i++) {
table[i] = i;
}
for (int i = 0; i < strSeed.length(); i++) {
int x = table[strSeed[i] - '!'];
seed_temp = seed_temp * C + table[x];
for (int j = 0; j < 11; j++) {
shuffle_table(table, x, j, C);
}
if (i != 0 && (i + 1) % 5 == 0) {
seed ^= seed_temp;
seed_temp = 0;
}
}
if (seed_temp != 0) {
seed ^= seed_temp;
}
return long(seed);
}
const String realStr = "おっぱいそんちくびんびん";
const String oppaiElements[] = {"おっ", "ぱい", "そん", "ちく", "びん", "びん"};
void shuffle(byte *arr, size_t nmemb)
{
byte tmp;
size_t n = nmemb;
while ( n > 1 ) {
size_t k = random(n--);
tmp = arr[n];
arr[n] = arr[k];
arr[k] = tmp;
}
}
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10);
}
randomSeed(createSeed());
}
void loop() {
String sp = " ";
String testStr = "";
int count = 0;
while (testStr.compareTo(realStr) != 0) {
++count;
byte numList[] = {0, 1, 2, 3, 4, 5};
shuffle(numList, sizeof(numList));
testStr = "";
for (int i = 0; i < 6; i++) {
testStr += oppaiElements[numList[i]];
}
Serial.println(String(count) + sp + testStr);
}
Serial.print(String("おめでとうございます!") + sp);
Serial.println(String("あなたは") + String(count) + "回目に" + String(realStr) + "しました。");
Serial.println("end");
Serial.flush();
while (true) {
delay(10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment