Advertisement
musifter

AoC day 14 (pt1), Perl

Dec 14th, 2020 (edited)
1,466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.43 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. no warnings qw(portable);
  6.  
  7. use List::AllUtils  qw(sum);
  8.  
  9. my %mem;
  10. my $and_mask;
  11. my $or_mask;
  12.  
  13. while (<>) {
  14.     if (m#mask = ([10X]+)#) {
  15.         $or_mask  = eval( "0b" . ($1 =~ y/X/0/r) );
  16.         $and_mask = eval( "0b" . ($1 =~ y/X/1/r) );
  17.     } elsif (m#mem\[(\d+)\] = (\d+)#) {
  18.         $mem{$1} = ($2 | $or_mask) & $and_mask;
  19.     }
  20. }
  21.  
  22. print "Part 1: ", sum( values %mem ), "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement