From 4bb6f8d06c0e384f3394012b1d48da58ed28cc5e Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Sun, 12 Dec 2021 01:24:32 +0300 Subject: 2020, tracking --- 2020/day3/toboggans.pl | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2020/day3/toboggans.pl (limited to '2020/day3/toboggans.pl') 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 @@ +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"); -- cgit v1.2.3-70-g09d2