Created
April 17, 2009 17:50
-
-
Save perigrin/97156 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
#!/usr/bin/perl | |
use strict; | |
use 5.10.0; | |
use LWP::Simple; | |
use JSON::Any; | |
use XML::Simple; | |
die 'Must provide starting URL' unless @ARGV; | |
solve_puzzle( $ARGV[0] ); | |
sub get_puzzle { | |
my ($url) = @_; | |
my $data = get $url; | |
say "$url\n$data"; | |
return XMLin($data) if ( $data =~ /</g ); | |
return JSON::Any->decode($data); | |
} | |
sub solve_puzzle { | |
my ($url) = @_; | |
my $data = get_puzzle($url); | |
my $params = $data->{params}; | |
my $op = delete $params->{op}; | |
delete $params->{result}; | |
my $result = __PACKAGE__->$op($params); | |
$url = "$data->{url}?op=$op;result=$result;" . join ';', map { | |
ref $params->{$_} | |
? 'list=' . join ',', @{ $params->{$_} } | |
: "$_=$params->{$_}"; | |
} keys %$params; | |
solve_puzzle($url); # this will eventually blow up the stack | |
} | |
# | |
# OPs | |
# | |
sub add { return $_[1]->{operand1} + $_[1]->{operand2} } | |
sub strip { | |
( my $str = $_[1]->{original} ) =~ s/\Q$_[1]->{remove}\E//g; | |
return $str; | |
} | |
sub count { return scalar @{ $_[1]->{list} } } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment