# Advent of Code Day 3 - the first modulo

#### Part 1

.. and the first 2D map!

Which is quite simple to parse in Perl (or Perl makes parsing simple?)

We just need to look ahead in the map and search for trees.

``````my @map = map { chomp; [split(//,\$_)] } <STDIN>;
my \$w = \$map->@*;

my \$trees;
my \$c=0;
my \$r=0;
while (my \$pos = \$map[\$r]->[\$c]) {
\$trees++ if \$pos eq '#';
\$c = (\$c + 3) % \$w;
\$r++;
}
say \$trees;``````

Instead of repeating the pattern (as hinted in the instruction, ts, ts), we of course use `modulo` to keep the horizontal counter inside the map definition.

I still managed to waste at least 5 minutes to a scoping bug:

``````while (my \$pos = \$map[\$r]->[\$c]) {
\$trees++ if \$pos eq '#';
my \$c = (\$c + 3) % \$w;
\$r++;
}``````

Notice the `my` before the assignment to `\$c`. Thanks to this I was always checking the first column, for not very correct results...

#### Part 2

For part two we just need to check a few (5) different ways to calculate where we should look for a tree, so we pack those instructions into an array, and adapt the lookahead to use these values instead of the hardcoded ones.

``````my @map = map { chomp; [split(//,\$_)] } <STDIN>;
my \$w = \$map->@*;

my @slopes=([1,1],[3,1],[5,1],[7,1],[1,2]);

my \$prod=1;
for my \$slope (@slopes) {
my \$trees;
my \$c=0;
my \$r=0;
while (my \$pos = \$map[\$r]->[\$c]) {
\$trees++ if \$pos eq '#';
\$c = (\$c + \$slope->) % \$w;
\$r += \$slope->;
}
\$prod*=\$trees;
}
say \$prod;``````

The basic logic stays the same, which would probably look nicer if I would have moved it into a function.

I wasted a few minuts on the second part because I was running the file `03_2.pl` but put my changes into an old, buggy version of `03_1.pl` - and I was very annoyed why the loop did no stop...