# ETOOBUSY đźš€ minimal blogging for the impatient

# AoC 2022/15 - Diamonds are squares in disguise

**TL;DR**

On with Advent of Code puzzle 15 from 2022: thereâ€™s always to learn from the Megathread.

This dayâ€™s solution was a bad exercise in brute force for me, so Iâ€™m not commenting my solution. As a matter of fact, I deleted my solution to the second part actually, and opted for reimplementing it in a more rational way (although not completely correct).

The full solution is available, of course.

The gist of how part 2 *should be addressed* is to avoid going fully
brutal and only figure out intersections of the outer â€śshellsâ€ť
(actually, perimeters) for each sensorâ€™s reach.

This can also be combined with a simple change of coordinates that turns
the diamond-shaped ranges into plain squares, which are *extremely* easy
to search for intersections.

So hereâ€™s my part 2:

```
sub part2 ($inputs, $midsize) {
my $size = $midsize * 2;
my @t-rex = $inputs.map: ->($x, $y, $bx, $by) {
my @r = (1 + ($x - $bx, $y - $by)Â».abs.sum) Â«*Â» (-1, 1);
(($x + $y) Â«+Â» @r, ($x - $y) Â«+Â» @r).flat.Array;
};
for @t-rex -> $h {
my ($hx-min, $hx-max, $hy-min, $hy-max) = |$h;
for @t-rex -> $v {
next if $h === $v;
my ($vx-min, $vx-max, $vy-min, $vy-max) = |$v;
PAIR:
for ($hy-min, $hy-max) X ($vx-min, $vx-max) -> ($hy, $vx) {
next unless $vy-min <= $hy <= $vy-max
&& $hx-min <= $vx <= $hx-max;
next PAIR if ($vx + $hy) % 2;
for @t-rex -> ($cx-min, $cx-max, $cy-min, $cy-max) {
next PAIR if $cx-min < $vx < $cx-max
&& $cy-min < $hy < $cy-max;
}
my $x = ($vx + $hy) div 2;
next PAIR unless 0 <= $x <= $size;
my $y = ($vx - $hy) div 2;
next PAIR unless 0 <= $y <= $size;
return $x * $size + $y;
}
}
}
return 'part2';
}
```

The `$midsize`

is the 10 or 2000000 value that should be otherwise
hardcoded.

After calculating the intersections, we go back in the â€śnormalâ€ť coordinates system and check if the intersection is within the bounds. When we find one such positionâ€¦ weâ€™re done.

How is this *not completely* correct? Wellâ€¦ there are four corner
cases, located at the very four corners of the search area, where the
missing beacon might be located, and this solution would not find it.

Easy to address and left as an exercise for the reader, have fun!

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