AoC 2022/10 - Cathode Ray Tube

TL;DR

On with Advent of Code puzzle 10 from 2022: thereโ€™s always a pixelated screen in Advent of Code!

So this puzzle eventually requires us to figure out a textual code in good olโ€™ ASCII art. Brilliant!

Getting the inputs shows my utter ignorance of how to do this properly/idiomatically:

sub get-inputs ($filename) {
   my $X = 1;
   return [
      1, $filename.IO.lines.map(
         {
            $_ eq 'noop'
               ?? $X + 0
               !! ($X + 0, ($X += .comb(/ \-? \d+ /)[0]) + 0);
         }
      ).flat.Slip
   ];
}

Whatever, it works. At each line, Iโ€™m immediately generating the values in the sequence. It took me a while (wellโ€ฆ a lot!) to figure out that I had to pepper this code with all the + 0 to avoid getting the container instead of the value. This is where my Perl accent is at its strongest, and where I miss Perl too.

Anyway.

With the sequence properly expanded, part 1 is easy to address:

sub part1 ($inputs) {
   my @targets = 20, 60, 100, 140, 180, 220;
   return (@targets ยซ*ยป $inputs[@targets ยซ-ยป 1]).sum;
}

Itโ€™s a nice occasion to use hyperstuff, yay!

The second part is slighly more challenging, but still manageable:

sub part2 ($inputs) {
   my @crt = '';
   for $inputs.kv -> $i, $v {
      @crt.push: '' if $i %% 40;
      my $brush = @crt[*-1].chars;
      @crt[*-1] ~= ($v - 1 <= $brush <= $v + 1) ?? 'โ–ˆ' !! ' ';
      last if $i == 239;
   }
   return @crt.join("\n");
}

In this case, itโ€™s good to be able and use the full block to obtain the answer, instead of using characters like # and .. This is my output:

AoC 2022/10 solution to part 2 for my puzzle input

Ainโ€™t these blocks nice?!?

Full solution.

Stay safe folks!


Comments? Octodon, , GitHub, Reddit, or drop me a line!