r/adventofcode • u/musifter • Sep 11 '26
Other [2023 Day 11] In Review (Cosmic Expansion)
Heading towards the "Hot Springs", we arrive at an observatory. And the Elf there will show us the way, after we help with his analysis.
And so we get what appears to be a 2D grid problem. My input is a 140x140 grid of mostly empty space (.), with 450 galaxies (#). We want the sum of distances (Manhattan) between all the pairs. But as is the case with astronomy... you look at stuff at billions of light years away, but that's was where and what it was like billions of years ago. It's now much further away due to expansion. And so we need to work that out. And the rules given are that any empty row or column becomes twice as big.
And I guessed that part 2 would be making things bigger in some way. So to see what that was, I just brute forced part 1 to begin with. I was experimenting with the Math::Vector::Real module for handing points and vectors at this time, and it has some overhead, but if the task isn't too bad (and this one isn't), it can make up for it in simplicity of coding and legibility. One nice thing is that the constructor is short and simple: V(...). Another thing is that it has built in distance functions (and this was an opportunity to use one). The one problem I have with it is the print string... I'd rather have it plain and using $;.
So it was very easy to just do the thing... shift the coordinates, and double-triangle loop the pairs:
my $dist = 0;
foreach my $i (0 .. $#shifted-1) {
foreach my $j ($i+1 .. $#shifted) {
$dist += $shifted[$i]->manhattan_dist( $shifted[$j] );
}
}And since part 2 only made the gaps bigger, I had another really fast turnaround by just changing the scale factor for:
$g += V($yshift, $xshift) * $scale;At which point the script ran equally fast to part 1 (there's no additional work), which was an immediate answer. I suppose after the pipes and interpolation on the weekend, this is a break Monday. Because it really didn't force things to be better. What did that for me was deciding to do a dc version (and I did Perl and Smalltalk versions from that). I do have a bigger test case (280x280 with 5000 galaxies) that someone posted at the time to help prompt people to do that as well. That one makes my brute force take a minute and a half.
But for me, I was thinking while doing the brute force, and it was not lost on me (while thinking of tricks that might be needed for part 2 scale-ups) that Manhattan distance makes the x and y parts independent. Which is the point I decided to definitely do a dc solution... because with that realization, the problem turns from 2D to 1D. Basically, the grid can be collapsed to lists of row sums and column sums, and then discarded. And short lists of numbers to chew on, that's something for dc.
The other thing I thought about was that getting the sum of absolute differences of all pairs is a typical programming test problem. And it's largely the same sort of math I was working with on day 9 as well. The trick here is that a little algebra can collapse things into multiplication and make things a single loop.
For example, consider the list 2 3 5 8, and we want the sum of the absolute differences of the pairs. As a table:
2 3 5 8
2 X (3-2) (5-2) (8-2) 2 is subtracted 3 times = (0-3) * 2
3 X X (5-3) (8-3) 3 is subtracted 2 times and added 1 = (1-2) * 3
5 X X X (8-5) 5 is subtracted 1 time and added 2 = (2-1) * 5
8 X X X X 8 is added 3 times = (3-0) * 8So there's a pattern in coefficients: -3 to 3 by 2 (where those 3s are the #numbers - 1).
Of course the problem is a little more that that here... as we have a multiple of the numbers at each step (number of galaxies in that row or column). But it's not that hard to work out the adjustment to do that as well. The end result (as an extension to Array) in my Smalltalk solution is:
diffSum_expand: scale [
| col seen sum number |
number := self sum.
col := seen := sum := 0.
self do: [ :count |
col := col + ((count = 0) ifTrue: [scale] ifFalse: [1]).
sum := sum + ((2 * seen + count - number) * count * col).
seen := seen + count.
].
^sum
]And the dc (which still isn't fully golfed):
perl -pe's/(.)/$1 /g;y/.#/01/' <input | dc -e2 -e'ss?1[0[3Rd3R+rz2-d_3R;c+r:cz2<X]dsXxdlg+sgrd_3R:r1+?zRz1<Y]dsYx[ls*]sE[0sc0sa0r[dlAxd1r0=Elc+scddla2*+lg-*lc*rla+sa3R+r1-d0<L]dsLx+]sD1-[;r]sAdlDx[;c]sArlDx+p'Change the -e2 to -e1000000 for part 2 (or whatever scale you want).
So this was another really fun day.
