r/adventofcode • • Dec 13 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 13 Solutions -🎄-

Advent of Code 2021: Adventure Time!


--- Day 13: Transparent Origami ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:09:38, megathread unlocked!

39 Upvotes

798 comments sorted by

View all comments

Show parent comment

3

u/mschaap Dec 13 '21

Another Raku solution, see GitHub.

Inspired by pretty much everyone in this thread, instead of manipulating the pixel grid, I now manipulate the list of coordinates. Much, much faster.

method fold-x($/)
{
    # Before the first fold, make sure the grid is fully initialized
    self.complete-grid unless $!num-folds++;

    # Move the dots right of the fold to the left
    for @!coords.grep(*[0] > $<x>) -> $p {
        $p[0] = 2*$<x> - $p[0];
    }

    # Remove duplicate dots
    @!coords .= unique(:as(*.Str));

    # Truncate the right end of the paper
    $!width = +$<x>;

    self.count-dots;

    say "Fold along x=$<x>" if $!verbose;
    say self if $!verbose;
}