Advertisement
musifter

AoC day 14 (pt2), Perl

Dec 14th, 2020 (edited)
1,424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.74 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 indexes);
  8.  
  9. my %mem;
  10. my $or_mask;
  11. my @float;
  12.  
  13. sub recurse_write {
  14.     my ($loc, $val, @float) = @_;
  15.  
  16.     if (!@float) {
  17.         $mem{$loc} = $val;
  18.     } else {
  19.         my $bit  = 1 << (shift @float);
  20.         &recurse_write( $loc & ~$bit, $val, @float );
  21.         &recurse_write( $loc |  $bit,  $val, @float );
  22.     }
  23. }
  24.  
  25. while (<>) {
  26.     if (m#mask = ([10X]+)#) {
  27.         $or_mask  = eval( "0b" . ($1 =~ y/X/0/r) );
  28.         @float = indexes { $_ eq 'X' } reverse split( //, $1 );
  29.  
  30.     } elsif (m#mem\[(\d+)\] = (\d+)#) {
  31.         &recurse_write( $1 | $or_mask, $2, @float );
  32.     }
  33. }
  34.  
  35. print "Part 2: ", sum( values %mem ), "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement