http://www.perlmonks.org?node_id=11139698


in reply to Advent of Code, Day 15, golfed!

Quick golf trick: push() has a return value :)
Replace push@a,/./g;$r||=@a with $r||=push@a,/./g and save 3 strokes.

UPDATE: Oops. that what I get for not testing :(

Alternates:

push@a,@r=/./g}{print$s{-1+map{$u{$_}||=$a[$_]+$s{$g-1}for grep$a[$_]*!$s{$_},$g+@r,$g-!!($g%@r),$g-@r*($g>=@r),++$g*!!($g %@r);($g)=sort{$u{$a}-$u{$b}}keys%u;$s{$g}=delete$u{$g}}@a}

and

# problem description says "resembles a square" push@a,/./g}{print$s{-1+map{$u{$_}||=$a[$_]+$s{$g-1}for grep$a[$_]*!$s{$_},$g+$.,$g-!!($g%$.),$g-$.*($g>=$.),++$g*!!($g %$.);($g)=sort{$u{$a}-$u{$b}}keys%u;$s{$g}=delete$u{$g}}@a}