summaryrefslogtreecommitdiffstats
path: root/2020/day3/toboggans.pl
blob: ebf7ae4a3d9849c084f661ef5046ba1e51419db7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use strict;
use warnings;
# use Smart::Comments;
use DDP;

open my $fh, '<', "input" or die "no input present, $!";
chomp(my @forest = <$fh>);
close $fh;
my $len = scalar @forest;

my @right_ms = qw/1 3 5 7 1/;
my @down_ms = qw/ 1 1 1 1 2/;
my $runs = $#right_ms;

my $all_trees = 1;

foreach my $run (0..$runs) {

    my $toboggan = 0;
    my $trees = 0;
    my $down_mov = $down_ms[$run];
    my $right_mov = $right_ms[$run];

    for (my $line = 0; $line < $len; $line += $down_mov) {
        my $curr = substr($forest[$line], $toboggan, 1);
        if ($curr eq "#") {
            $trees++;
        }
        $toboggan = ($toboggan + $right_mov) % 31;
    }
    $trees ||= 1;
    $all_trees *= $trees;
    ### this run is
    ### $right_mov
    ### $down_mov
    ### got: $trees
    ### so far: $all_trees
}

print("$all_trees");