Advertisement
musifter

AoC 2022, day 25 (Perl)

Dec 25th, 2022
1,727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.56 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my %val = ( '=' => -2, '-' => -1, 0 => 0, 1 => 1, 2 => 2 );
  7. my %mod = ( 0 => 0, 1 => 1, 2 => 2, 3 => '=', 4 => '-' );
  8.  
  9. my $sum = 0;
  10. while (<>) {
  11.     chomp;
  12.  
  13.     my $pow = 1;
  14.     my $num = 0;
  15.     foreach my $dig (reverse split //) {
  16.         $num += $val{$dig} * $pow;
  17.         $pow *= 5;
  18.     }
  19.  
  20.     $sum += $num;
  21. }
  22.  
  23. print "Sum: $sum\n";
  24.  
  25. my @part1;
  26. while ($sum) {
  27.     my $dig = $sum % 5;
  28.     unshift( @part1, $mod{$dig} );
  29.     $sum = int( $sum / 5 ) + ($dig >= 3);
  30. }
  31.  
  32. print "Part 1: ", @part1, "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement