Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Advent of Code, Day 15, golfed!

by tybalt89 (Monsignor)
on Dec 18, 2021 at 02:44 UTC ( #11139698=note: print w/replies, xml ) Need Help??


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}

Replies are listed 'Best First'.
Re^2: Advent of Code, Day 15, golfed!
by Jasper (Chaplain) on Dec 18, 2021 at 10:32 UTC
    I don't think that will work because only the first row will be pushed into @a.
Re^2: Advent of Code, Day 15, golfed!
by Jasper (Chaplain) on Dec 19, 2021 at 04:39 UTC
    Combining that with another stroke saved by replacing the stupid ++$g, $g-1 stuff
    push@a,/./g}{print$s{-1+map{$u{$_}||=$a[$_]+$s{$g}for grep$a[$_]*!$s{$_},$g+$.,$g-($g%$.>0),$g-$.*($g>=$.),$g+($g%$. <$.-1);($g)=sort{$u{$a}-$u{$b}}keys%u;$s{$g}=delete$u{$g}}@a}
Re^2: Advent of Code, Day 15, golfed!
by Jasper (Chaplain) on Dec 19, 2021 at 03:46 UTC
    $. definitely works, how could I forget! :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11139698]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2023-01-28 00:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?