]> www.fi.muni.cz Git - aoc.git/blobdiff - 2020/6.pl
Moved 2020 to a subdir
[aoc.git] / 2020 / 6.pl
diff --git a/2020/6.pl b/2020/6.pl
new file mode 100755 (executable)
index 0000000..6749d06
--- /dev/null
+++ b/2020/6.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+my $field = do { local $/; <> };
+my $rows = $field =~ s/\s//g;
+my $flen = length $field;
+my $cols = $flen/$rows;
+
+print "Field has $flen bytes, in $rows rows and $cols cols\n";
+
+sub slope {
+       my ($colstep, $rowstep) = @_;
+
+       $colstep += $cols;
+
+       my $col = 0;
+       my $sum = 0;
+       my $row = 0;
+       while ($row < $rows) {
+               print "row $row\n";
+               $sum++ if substr($field, $row*$cols + $col, 1) eq '#';
+               $col += $colstep;
+               $col %= $cols;
+               $row += $rowstep;
+       }
+       print "$colstep x $rowstep, sum=$sum\n";
+       return $sum;
+}
+
+print "Total: ",
+       slope(1, 1)
+       * slope(3, 1)
+       * slope(5, 1)
+       * slope(7, 1)
+       * slope(1, 2),
+       "\n";
+