本帖最后由 523066680 于 2017-8-4 22:30 编辑
回复 64# avgBullsCows
感谢neo提供的最优搜索树,省去了筛选+处理数组的过程,本地效率大大提升。(后来我发现这个问题即使运行时效率提高了,网络不熟悉的话还是要吃大亏啊)
读取 JSON 进行刷题的 Perl 代码:- =info
- 523066680 2017-08
- =cut
-
- use JSON;
- use IO::Handle;
- use File::Slurp;
- use LWP::UserAgent;
- use Data::Dumper;
- use Time::HiRes qw/sleep/;
- STDOUT->autoflush(1);
-
- our $user = "vic3";
- our $pass = "tempcode";
- our $website = 'http://www.codetiger.win/extra/API.php';
- our $ua = LWP::UserAgent->new( keep_alive=>1, agent => 'Mozilla/5.0', timeout => 0.2 );
-
- my $json;
- my $tree;
- $json = read_file("crushBullsCows.json");
- $json=~s/\'/\"/g;
- $tree = decode_json($json);
-
- AGAIN:
- my $ref = $tree;
- my $guess = $ref->{'xx'};
- my $AB;
- my ($curr, $prev) = (undef, undef);
-
- while (1)
- {
- ($AB, $curr) = post_num( $guess );
- print " [$AB] [ $guess ] $curr\n";
-
- if ( $AB eq "40" ) {
- print "Success $guess\n\n";
- last;
- }
-
- if (defined $prev and ($curr ne $prev) )
- {
- print "Tokens changed\n [$AB] [ @$guess ] $curr\n";
- goto AGAIN;
- }
-
- #update guess number, and $ref
- for my $e ( @{$ref->{'c'}})
- {
- if ( exists $e->{$AB} )
- {
- #print $e->{$AB};
- $guess = $e->{$AB};
- $ref = $e;
- last;
- }
- }
- $prev = $curr;
- }
-
- goto AGAIN;
-
- sub post_num
- {
- our($ua, $website, $user, $pass);
- my $n = shift;
- my $res;
- my $data;
- my $text;
-
- while (1)
- {
- $res =
- $ua->post(
- $website,
- [ username=>$user, password =>$pass, number=>$n, send=>'answer' ]
- );
- $text = $res->content();
- if ( $text =~/"A":\d,"B":\d/ ) { last }
- print ".";
- sleep 0.01;
- }
-
- #print $res->content(), " - $n\n";
- $data = decode_json( $res->content() );
- return $data->{A} . $data->{B}, $data->{tokens};
- }
复制代码
|