Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- no warnings qw(portable);
- use List::AllUtils qw(sum indexes);
- my %mem;
- my $or_mask;
- my @float;
- sub recurse_write {
- my ($loc, $val, @float) = @_;
- if (!@float) {
- $mem{$loc} = $val;
- } else {
- my $bit = 1 << (shift @float);
- &recurse_write( $loc & ~$bit, $val, @float );
- &recurse_write( $loc | $bit, $val, @float );
- }
- }
- while (<>) {
- if (m#mask = ([10X]+)#) {
- $or_mask = eval( "0b" . ($1 =~ y/X/0/r) );
- @float = indexes { $_ eq 'X' } reverse split( //, $1 );
- } elsif (m#mem\[(\d+)\] = (\d+)#) {
- &recurse_write( $1 | $or_mask, $2, @float );
- }
- }
- print "Part 2: ", sum( values %mem ), "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement