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, Twitter, GitHub, Reddit, or drop me a line!