diff options
author | Andinus <andinus@nand.sh> | 2020-12-04 20:21:18 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2020-12-04 20:21:18 +0530 |
commit | 26abda4d486c2e9efc276d6b7e85f8ce5d009828 (patch) | |
tree | 988c0b3d980494761e7e4ec5f85f48d8a8fa316f /2020/day-03/day-03.raku | |
parent | f2da3d2696546d1b12e9eb6a30bab31fefbba4ee (diff) | |
download | aoc-26abda4d486c2e9efc276d6b7e85f8ce5d009828.tar.gz |
Add day-03 solution
Diffstat (limited to '2020/day-03/day-03.raku')
-rwxr-xr-x | 2020/day-03/day-03.raku | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/2020/day-03/day-03.raku b/2020/day-03/day-03.raku new file mode 100755 index 0000000..c8656ac --- /dev/null +++ b/2020/day-03/day-03.raku @@ -0,0 +1,50 @@ +#!/usr/bin/env raku + +sub MAIN ( + # Part to run. + Int $part where * == 1|2 = 1 +) { + my @input = "input".IO.lines>>.comb; + + if $part == 1 { + say "Part 1: ", count-trees(@input, 3, 1); + } elsif $part == 2 { + my @trees = [{ right => 1, down => 1 }, + { right => 3, down => 1 }, + { right => 5, down => 1 }, + { right => 7, down => 1 }, + { right => 1, down => 2 },].map( + {count-trees(@input, $_<right>, $_<down>)} + ); + say "Part 2: ", [*] @trees; + } +} + +sub count-trees ( + @input, + Int $right, + Int $down +) { + # Start at top left position. + my $x = 0; + my $y = 0; + my $trees = 0; + + my $x-max = @input[0].elems - 1; + my $y-max = @input.elems - 1; + + loop { + $trees++ if @input[$y][$x] eq '#'; + + $x += $right; + $y += $down; + + # Cannot let $x become greater than $x-max because that'll produce + # error when we try to access @input with those positions. So, we + # wrap it around because the same map is repeated. + $x -= $x-max + 1 if $x > $x-max; + + last if $y-max < $y; + } + return $trees; +} |