]> www.fi.muni.cz Git - aoc2020.git/blob - 27.pl
Task 9 Perl Golf-style
[aoc2020.git] / 27.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 no warnings 'portable';
5
6 my @mem;
7
8 my ($andm, $orm);
9 while (<>) {
10         chomp;
11         if (/mask = (\S+)/) {
12                 ($orm, $andm) = ($1, $1);
13                 $andm =~ s/X/1/g; $andm = oct "0b$andm";
14                 $orm  =~ s/X/0/g; $orm  = oct "0b$orm";
15                 print "andm=$andm\n orm=$orm\n";
16                 next;
17         }
18         if (/mem\[(\d+)\] = (\d+)/) {
19                 $mem[$1] = ($2 | $orm) & $andm;
20                 print "mem[$1] = $2 => $mem[$1]\n";
21         }
22 }
23
24 my $sum;
25 for (@mem) { $sum += $_ if defined $_ };
26 print "Sum=$sum\n";