summaryrefslogtreecommitdiffstats
path: root/2020/day3/toboggans.pl
diff options
context:
space:
mode:
authorYigit Sever2021-12-13 10:38:11 +0300
committerYigit Sever2021-12-13 10:38:11 +0300
commit74b27ccca31bb757c737dd7fdc02f513f57561b2 (patch)
treee27db4cd0873c81a53d32277446d926d176304e0 /2020/day3/toboggans.pl
parent3919f90cfbfbba26c8e39f979280649f5e08aea8 (diff)
parentac8125750abed263619da4cc6d653bb5ab76f007 (diff)
downloadaoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.tar.gz
aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.tar.bz2
aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.zip
Merge remote-tracking branch 'origin/main'
Diffstat (limited to '2020/day3/toboggans.pl')
-rw-r--r--2020/day3/toboggans.pl40
1 files changed, 40 insertions, 0 deletions
diff --git a/2020/day3/toboggans.pl b/2020/day3/toboggans.pl
new file mode 100644
index 0000000..ebf7ae4
--- /dev/null
+++ b/2020/day3/toboggans.pl
@@ -0,0 +1,40 @@
1use strict;
2use warnings;
3# use Smart::Comments;
4use DDP;
5
6open my $fh, '<', "input" or die "no input present, $!";
7chomp(my @forest = <$fh>);
8close $fh;
9my $len = scalar @forest;
10
11my @right_ms = qw/1 3 5 7 1/;
12my @down_ms = qw/ 1 1 1 1 2/;
13my $runs = $#right_ms;
14
15my $all_trees = 1;
16
17foreach my $run (0..$runs) {
18
19 my $toboggan = 0;
20 my $trees = 0;
21 my $down_mov = $down_ms[$run];
22 my $right_mov = $right_ms[$run];
23
24 for (my $line = 0; $line < $len; $line += $down_mov) {
25 my $curr = substr($forest[$line], $toboggan, 1);
26 if ($curr eq "#") {
27 $trees++;
28 }
29 $toboggan = ($toboggan + $right_mov) % 31;
30 }
31 $trees ||= 1;
32 $all_trees *= $trees;
33 ### this run is
34 ### $right_mov
35 ### $down_mov
36 ### got: $trees
37 ### so far: $all_trees
38}
39
40print("$all_trees");