]> www.fi.muni.cz Git - aoc2020.git/blob - 6.pl
Task 9 Perl Golf-style
[aoc2020.git] / 6.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 my $field = do { local $/; <> };
6 my $rows = $field =~ s/\s//g;
7 my $flen = length $field;
8 my $cols = $flen/$rows;
9
10 print "Field has $flen bytes, in $rows rows and $cols cols\n";
11
12 sub slope {
13         my ($colstep, $rowstep) = @_;
14
15         $colstep += $cols;
16
17         my $col = 0;
18         my $sum = 0;
19         my $row = 0;
20         while ($row < $rows) {
21                 print "row $row\n";
22                 $sum++ if substr($field, $row*$cols + $col, 1) eq '#';
23                 $col += $colstep;
24                 $col %= $cols;
25                 $row += $rowstep;
26         }
27         print "$colstep x $rowstep, sum=$sum\n";
28         return $sum;
29 }
30
31 print "Total: ",
32         slope(1, 1)
33         * slope(3, 1)
34         * slope(5, 1)
35         * slope(7, 1)
36         * slope(1, 2),
37         "\n";
38