Created
March 20, 2022 15:32
-
-
Save crazymonkyyy/fcf6cca3417c1c74ddf55991b26fc249 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import basic; | |
enum count=6; | |
enum nullchar='-'; | |
enum correct='^'; | |
enum misplaced='?'; | |
alias format=char[count]; | |
format hint(format geuss,format answer){ | |
format o; | |
foreach(i;0..count){ | |
o[i]=nullchar; | |
} | |
foreach(i;0..count){ | |
if(geuss[i]==answer[i]){ | |
answer[i]=nullchar; | |
geuss[i]=correct; | |
o[i]=correct; | |
} | |
} | |
foreach(i;0..count){ | |
foreach(j;0..count){ | |
if(geuss[i]==answer[j]){ | |
answer[j]=nullchar; | |
geuss[i]=correct; | |
o[i]=misplaced; | |
} | |
}} | |
return o; | |
} | |
format to(T:format)(string s){ | |
format o=s; | |
return o; | |
} | |
//unittest{ | |
// void test(T)(T a,T b){ | |
// a.write; " ,".write; b.writeln; | |
// hint(a.to!format,b.to!format).writeln; | |
// } | |
// test("1234","1234"); | |
// test("1111","2221"); | |
// test("1122","1221"); | |
// test("1122","2341"); | |
//} | |
char random(){ | |
enum set="123456789"; | |
return set[uniform(0,set.length)]; | |
} | |
//unittest{ | |
// foreach(i;0..100){ | |
// random.writeln; | |
//}} | |
format randomarray(){ | |
format o; | |
foreach(i;0..count){ | |
o[i]=random; | |
} | |
return o; | |
} | |
//unittest{ | |
// foreach(i;0..10){ | |
// randomarray.writeln; | |
//}} | |
auto filterdown(format guess_,format[] answer){ | |
static format guess; | |
guess=guess_; | |
auto hint_(format answer){ | |
return hint(guess,answer); | |
} | |
auto o=answer.sort!((a,b)=>hint_(a)>hint_(b)).chunkBy!hint_.array; | |
o.schwartzSort!(a=>-a[1].array.length); | |
hint_(o[0][1].array[0]).writeln; | |
return o[0][1]; | |
} | |
//unittest{ | |
// format[50] foo; | |
// foreach(i;0..foo.length){ | |
// foo[i]=randomarray; | |
// } | |
// filterdown("1122".to!format,foo); | |
//} | |
void main(){ | |
format[] answers; | |
foreach(i;0..700){ | |
answers~=randomarray; | |
} | |
while(true){ | |
answers=filterdown(readln[0..count].to!format,answers).array[]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment