diff options
author | Andinus <andinus@nand.sh> | 2020-12-12 19:55:28 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2020-12-12 19:55:28 +0530 |
commit | 7a7077baa8ee25449c249dc1adaef9a5b273cfb9 (patch) | |
tree | f84fe8a957b3ef6650f6984202edbc83f8edde38 | |
parent | a2f46a9f4b4122929d8340bc20f42322bd4aefd6 (diff) | |
download | aoc-7a7077baa8ee25449c249dc1adaef9a5b273cfb9.tar.gz |
Add an interesting solution to README
-rw-r--r-- | 2020/day-03/README.org | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/2020/day-03/README.org b/2020/day-03/README.org index 56b3863..165898a 100644 --- a/2020/day-03/README.org +++ b/2020/day-03/README.org @@ -160,3 +160,37 @@ my @trees = [{ right => 1, down => 1 }, ); say "Part 2: ", [*] @trees; #+END_SRC +* Other solution +I found [[https://old.reddit.com/r/adventofcode/comments/k61rta/2020_day_3_raku/][this solution]] by [[https://old.reddit.com/user/cggoebel][cggoebel]] on reddit. It makes use of infinite +lazy lists, that way you don't have to check for boundary. I'll paste +the solution here. + +No license was attached to the code in that post. This belongs to +u/cggoebel. +#+BEGIN_SRC raku +use v6.d; + +my @input = 'input'.IO.lines; +my @map; + +for @input -> $line { + @map.push(($line.comb xx *).flat); +} + +sub detect_collisions (Int $sx, Int $sy) { + my $rval = 0; + loop (my ($x,$y)=($sx,$sy); $y < @map.elems; $x+=$sx, $y+=$sy) { + $rval++ if @map[$y][$x] eq '#'; + } + return $rval +} + +say "Part One: {detect_collisions(3,1)}"; + +my @collision; +for 1,1, 3,1, 5,1, 7,1, 1,2 -> $x, $y { + @collision.push(detect_collisions($x, $y)); +} + +say "Part Two: " ~ [*] @collision; +#+END_SRC |