]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 14
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Mon, 14 Dec 2020 09:41:09 +0000 (10:41 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Mon, 14 Dec 2020 09:41:09 +0000 (10:41 +0100)
27.pl [new file with mode: 0755]
28.pl [new file with mode: 0755]

diff --git a/27.pl b/27.pl
new file mode 100755 (executable)
index 0000000..a17a382
--- /dev/null
+++ b/27.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/perl -w
+
+use strict;
+no warnings 'portable';
+
+my @mem;
+
+my ($andm, $orm);
+while (<>) {
+       chomp;
+       if (/mask = (\S+)/) {
+               ($orm, $andm) = ($1, $1);
+               $andm =~ s/X/1/g; $andm = oct "0b$andm";
+               $orm  =~ s/X/0/g; $orm  = oct "0b$orm";
+               print "andm=$andm\n orm=$orm\n";
+               next;
+       }
+       if (/mem\[(\d+)\] = (\d+)/) {
+               $mem[$1] = ($2 | $orm) & $andm;
+               print "mem[$1] = $2 => $mem[$1]\n";
+       }
+}
+
+my $sum;
+for (@mem) { $sum += $_ if defined $_ };
+print "Sum=$sum\n";
diff --git a/28.pl b/28.pl
new file mode 100755 (executable)
index 0000000..edf339e
--- /dev/null
+++ b/28.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl -w
+
+use strict;
+no warnings 'portable';
+
+my %mem;
+
+my ($floatm, $orm);
+while (<>) {
+       chomp;
+       if (/mask = (\S+)/) {
+               ($orm, $floatm) = ($1, $1);
+               $floatm =~ s/1/0/g;
+               $orm  =~ s/X/0/g; $orm  = oct "0b$orm";
+               next;
+       }
+       if (/mem\[(\d+)\] = (\d+)/) {
+               my $data = $2;
+               for my $addr (addr2list($1|$orm, $floatm)) {
+                       $mem{$addr} = $data;
+                       # print "mem[$addr=", pack("b*", $addr),"] = $2\n";
+               }
+       }
+}
+
+my $sum;
+for (keys %mem) { $sum += $mem{$_} };
+print "Sum=$sum\n";
+
+sub addr2list {
+       my ($addr, $mask) = @_;
+       if ($mask =~ s/X/0/) {
+               my $off = length $';
+               return (addr2list($addr | (1 << $off), $mask),
+                       addr2list($addr & ~(1 << $off), $mask));
+       } else {
+               return ($addr);
+       }
+}