Day 10: Hoof It

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • vole@lemmy.world
    link
    fedilink
    English
    arrow-up
    2
    ·
    17 days ago

    Raku

    Pretty straight-forward problem today.

    sub MAIN($input) {
        my $file = open $input;
        my @map = $file.slurp.trim.lines>>.comb>>.Int;
    
        my @pos-tracking = [] xx 10;
        for 0..^@map.elems X 0..^@map[0].elems -> ($row, $col) {
            @pos-tracking[@map[$row][$col]].push(($row, $col).List);
        }
    
        my %on-possible-trail is default([]);
        my %trail-score-part2 is default(0);
        for 0..^@pos-tracking.elems -> $height {
            for @pos-tracking[$height].List -> ($row, $col) {
                if $height == 0 {
                    %on-possible-trail{"$row;$col"} = set ("$row;$col",);
                    %trail-score-part2{"$row;$col"} = 1;
                } else {
                    for ((1,0), (-1, 0), (0, 1), (0, -1)) -> @neighbor-direction {
                        my @neighbor-position = ($row, $col) Z+ @neighbor-direction;
                        next if @neighbor-position.any < 0 or (@neighbor-position Z>= (@map.elems, @map[0].elems)).any;
                        next if @map[@neighbor-position[0]][@neighbor-position[1]] != $height - 1;
                        %on-possible-trail{"$row;$col"} ∪= %on-possible-trail{"{@neighbor-position[0]};{@neighbor-position[1]}"};
                        %trail-score-part2{"$row;$col"} += %trail-score-part2{"{@neighbor-position[0]};{@neighbor-position[1]}"};
                    }
                }
            }
        }
    
        my $part1-solution = @pos-tracking[9].map({%on-possible-trail{"{$_[0]};{$_[1]}"}.elems}).sum;
        say "part 1: $part1-solution";
    
        my $part2-solution = @pos-tracking[9].map({%trail-score-part2{"{$_[0]};{$_[1]}"}}).sum;
        say "part 2: $part2-solution";
    }