AoC day 7 part 1. An inappropriate (and slow) solution, but regex abuse is always fun. The first part draws full beams on the grid; the second part counts the number of splits by looking for all
| with a
^ below.
use v5.36;<br><br>my $grid = do { local $/; readline };<br><br>my $w = index($grid, "\n");<br>my $wl = $w - 1;<br><br>#[url=https://infosec.exchange/tags/say]say[/url] $grid;<br><br>1 while $grid =~ s{<br> [S|] .{$wl}<br> (<br> (?= \.\^ )<br> |<br> .<br> \^?<br> )<br> \K \.<br>}{|}xsng;<br><br>#[url=https://infosec.exchange/tags/say]say[/url] $grid;<br>say scalar(() = $grid =~ m{ [S|] (?= .{$w} \^ ) }xsg);<br>
#
AdventOfCode #
perl #
regex
Füsilier Breitlinger
in reply to Füsilier Breitlinger • • •Sensitive content
AoC day 7, part 2. In principle, this problem is eminently regexable: All steps a beam takes can be described by a regex,
^corresponds to choice/alternative, and the number of ways the regex matches is the solution. In practice, this takes forever. I tried to speed things up with embedded code blocks and a cache, but it quickly gets overly complex and I suspect there may be bugs lurking in the interaction between embedded code blocks, closures,local, and backtracking.In the end I gave up and rewrote the regex as direct code with manual backtracking. I guess the cache makes it an instance of "dynamic programming".
PS: The array is called "alt", not "Alt". I had to add a weird space after
@to prevent Mastodon from changing the case and breaking the code.#AdventOfCode #perl