Advertisement
Guest User

Untitled

a guest
Dec 18th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.65 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. $filename = "18.txt";
  4.  
  5. open(FILE, $filename);
  6. @data = <FILE>;
  7. close(FILE);
  8.  
  9. $sum = 0;
  10. $sumb = 0;
  11.  
  12. foreach $line (@data) {
  13.         $line =~ s/[^0-9\+\*\(\)]*//sgi; # Condition the input
  14.         $sum = $sum + domath($line); #part1
  15.         $sumb = $sumb + domathb($line); #part2
  16. }
  17.  
  18. print "P1: ".$sum."\n";
  19. print "P2: ".$sumb."\n";
  20.  
  21.  
  22.  
  23. sub domath() {
  24.  
  25.         my $inp = $_[0];
  26.         my $s = "";
  27.         my $p = "";
  28.         my $matha = "";
  29.         my $mathb = "";
  30.         my $newinp;
  31.  
  32. while ($inp =~ m/\(([^()]+)\)/) { #Run innermost parenthesis first
  33.         $s = domath($1);
  34.         $p = $1;
  35.         $p =~ s/\(/\\\(/sgi;
  36.         $p =~ s/\)/\\\)/sgi;
  37.         $p =~ s/\+/\\\+/sgi;
  38.         $p =~ s/\*/\\\*/sgi;
  39.         $inp =~ s/\($p\)/$s/; #Replace inner parenthesis with result
  40. }
  41.  
  42. while ($inp =~ m/([+*])/) { #No parenthesis left - run math parser until no operators are left.
  43.  
  44.         $char = $1;
  45.         $matha = $inp;
  46.         $matha =~ s/^(\d+)(.*)$/$1/; #Leftmost digit
  47.         $mathb = $inp;
  48.         $mathb =~ s/^(\d+)[+*](\d+)(.*)$/$2/; #Leftmost digit after operator
  49.  
  50.         if ($char eq "+") {
  51.                 $newinp = $matha + $mathb;
  52.         }
  53.         else
  54.         {
  55.                 $newinp = $matha * $mathb;
  56.         }
  57.  
  58.         $char =~ s/\+/\\\+/sgi;
  59.         $char =~ s/\*/\\\*/sgi;
  60.  
  61.         $inp =~ s/$matha$char$mathb/$newinp/; #Replace expression with result.
  62.         }
  63.  
  64. return $inp;
  65. }
  66.  
  67.  
  68. sub domathb() {
  69.  
  70. my $inp = $_[0];
  71. my $s = "";
  72. my $p = "";
  73. my $matha = "";
  74. my $mathb = "";
  75. my $newinp;
  76.  
  77.  
  78. while ($inp =~ m/\(([^()]+)\)/) { # same as before - run inner parenthesis first
  79.         $s = domathb($1);
  80.         $p = $1;
  81.         $p =~ s/\(/\\\(/sgi;
  82.         $p =~ s/\)/\\\)/sgi;
  83.         $p =~ s/\+/\\\+/sgi;
  84.         $p =~ s/\*/\\\*/sgi;
  85.         $inp =~ s/\($p\)/$s/; #replace inner parenthesis with result
  86. }
  87.  
  88. while ($inp =~ m/([+])/) { # Math parser for + only - run until theres no +'s left
  89.  
  90.         $matha = $inp;
  91.         $matha =~ s/^(.*?)([0-9]+)\+([0-9]+)(.*?)$/$2/; # Digits before +
  92.         $mathb = $inp;
  93.         $mathb =~ s/^(.*?)([0-9]+)\+([0-9]+)(.*?)$/$3/; # Digits after +
  94.  
  95.         $newinp = $matha + $mathb;
  96.  
  97.         $inp =~ s/$matha\+$mathb/$newinp/; #Replace expression with result
  98. }
  99.  
  100. while ($inp =~ m/([*])/) { # Math parser for * only - run until theres no *'s left
  101.  
  102.         $matha = $inp;
  103.         $matha =~ s/^(.*?)([0-9]+)\*([0-9]+)(.*?)$/$2/; # Digits before *
  104.         $mathb = $inp;
  105.         $mathb =~ s/^(.*?)([0-9]+)\*([0-9]+)(.*?)$/$3/; # Digits after *
  106.  
  107.         $newinp = $matha * $mathb;
  108.  
  109.         $inp =~ s/$matha\*$mathb/$newinp/;
  110. }
  111.  
  112. return $inp;
  113. }
  114.  
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement