mtve-versert.pl 1008 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #! /usr/bin/perl -w
  2. # from http://www.frox25.no-ip.org/~mtve/wiki/Versert.html
  3. use strict;
  4. open my ($f), $ARGV[0] or die "usage: $0 file\n";
  5. my $txt = do { local $/; <$f> };
  6. my @arr = map [/./g], $txt =~ /.*\n/g;
  7. my ($x, $y, $dx, $dy, $px, $py, $a, $b, $t) = (0, 0, 1, 0, 0, 0, 0, 0);
  8. $|++;
  9. while (1) {
  10. $_ = $arr[$y][$x];
  11. # print "x=$x y=$y dx=$dx dy=$dy px=$px py=$py a=$a b=$b doing $_\n";
  12. die if !defined;
  13. /\d/ ? $a = $_ :
  14. /\+/ ? $b += $a :
  15. /-/ ? $b -= $a :
  16. /\*/ ? $b *= $a :
  17. /~/ ? ($a, $b) = ($b, $a) :
  18. />/ ? $a < $b && (($a, $b) = ($b, $a)) :
  19. /</ ? $a > $b && (($a, $b) = ($b, $a)) :
  20. /\// ? ($dx, $dy) = (-$dy, -$dx) :
  21. /\\/ ? ($dx, $dy) = ($dy, $dx) :
  22. /\@/ ? exit :
  23. /#/ ? $b == 0 && ($x += $dx, $y += $dy) :
  24. /\./ ? print chr $a :
  25. /:/ ? print $a :
  26. /,/ ? defined ($t = getc) && ($a = ord $t) :
  27. /;/ ? $a = <> :
  28. /\{/ ? $b = ord $arr[$py][$px] :
  29. /\|/ ? ($px += $a, $py += $b) :
  30. /\}/ ? $arr[$py][$px] = chr $b :
  31. 1;
  32. $x += $dx;
  33. $y += $dy;
  34. $px = 0 if $px < 0;
  35. $py = 0 if $py < 0;
  36. }