Finally figured out AoC day 12. In the end, I gave up and looked up what I now know is a "union-find" algorithm. But at least I recognized the connection between AoC 12 and HM type inference/unification, I guess.
Also, I'm not sure what's going on with the fence logic here. I mean, I can prove it correct, but shouldn't there be a more readable/simple way to express it?
use List::Util qw(sum0);
my $board = do { local $/; readline };
my $n = index $board, "\n";
$board =~ tr/\n//d;
my @regions;
my $representative = sub ($p) {
my $repr = $p;
while (defined(my $next = $regions[$repr]{repr})) {
$repr = $next;
}
while ($p != $repr) {
my $rr = \$regions[$p]{repr};
($p, $$rr) = ($$rr, $repr);
}
$repr
};
my $merge = sub ($p, $q) {
$p = $representative->($p);
$q = $representative->($q);
return if $p == $q;
if ($regions[$p]{area} > $regions[$q]{area}) {
($p, $q) = ($q, $p);
}
my $rp = $regions[$p];
my $rq = $regions[$q];
$rp->{repr} = $q;
$rq->{area} += $rp->{area};
$rq->{peri} += $rp->{peri};
};
my @fence = (
4, 4, 4, 4,
0, 0, 2, 2,
0, 2, 2, 4,
-2, 0, -2, 0,
);
for my $p (0 .. length($board) - 1) {
my $t = substr $board, $p, 1;
my $c = $p % $n;
my @tbm;
my $fi = 0;
if ($p >= $n && $t eq substr $board, $p - $n, 1) {
push @tbm, $p - $n;
$fi |= 1;
}
$fi <<= 1;
if ($c > 0 && $t eq substr $board, $p - 1, 1) {
push @tbm, $p - 1;
$fi |= 1;
}
$fi <<= 1;
$fi |= 1 if $c > 0 && $p > $n && $t eq substr $board, $p - $n - 1, 1;
$fi <<= 1;
$fi |= 1 if $c < $n - 1 && $p >= $n && $t eq substr $board, $p - $n + 1, 1;
$regions[$p] = { area => 1, peri => $fence[$fi] };
$merge->($p, $_) for @tbm;
}
[#]for my $i (0 .. $#regions) {
[#] my $r = $regions[$i];
[#] next if defined $r->{repr};
[#] say substr($board, $i, 1) . ": $r->{area} * $r->{peri} = " . $r->{area} * $r->{peri};
[#]}
say sum0 map $->{area} * $->{peri}, grep !defined $_->{repr}, @regions;
#AdventOfCode #perl
=> More informations about this toot | View the thread
/etc/sudörs
=> More informations about this toot | View the thread
AoC day 5 in Perl. I'm not quite happy with the second loop. It feels a bit ad-hoc, like maybe there is a simpler algorithm waiting to be revealed. But it's not too bad, and it works:
use builtin qw(true);
my %after;
while (readline) {
chomp;
last if $_ eq '';
my ($fst, $snd) = /^ (\d+) \| (\d+) \z/xa
or die "Malformed input: $_";
push @{$after{$fst}}, $snd;
}
my $good_total = 0;
my $bad_total = 0;
while (readline) {
chomp;
my @pgs = split /,/;
my $ok = true;
my (%seen, %before);
for my $pg (@pgs) {
$seen{$pg} = true;
for my $x (@{$after{$pg} // []}) {
push @{$before{$x}}, $pg;
$ok &&= !$seen{$x};
}
}
my ($mid) = grep { @{$before{$_} // []} == $#pgs / 2 } @pgs;
${$ok ? \$good_total : \$bad_total} += $mid;
}
say $good_total;
say $bad_total;
#AdventOfCode #perl
=> More informations about this toot | View the thread
AoC day 4. Still pretty simple to solve with a couple of regexes. The hardest part was remembering to add the g flag to all matches.
$_ = do { local $/; readline };
my $n = index $_, "\n";
my $m = $n - 1;
my $o = $n + 1;
say
+ (() = /XMAS/g)
+ (() = /SAMX/g)
+ (() = /X(?=.{$n}M.{$n}A.{$n}S)/sg)
+ (() = /S(?=.{$n}A.{$n}M.{$n}X)/sg)
+ (() = /X(?=.{$m}M.{$m}A.{$m}S)/sg)
+ (() = /S(?=.{$m}A.{$m}M.{$m}X)/sg)
+ (() = /X(?=.{$o}M.{$o}A.{$o}S)/sg)
+ (() = /S(?=.{$o}A.{$o}M.{$o}X)/sg)
;
say
+ (() = /M(?=.{$o}A.{$o}S)(?=[^\n]M.{$m}A.{$m}S)/sg)
+ (() = /M(?=.{$o}A.{$o}S)(?=[^\n]S.{$m}A.{$m}M)/sg)
+ (() = /S(?=.{$o}A.{$o}M)(?=[^\n]M.{$m}A.{$m}S)/sg)
+ (() = /S(?=.{$o}A.{$o}M)(?=[^\n]S.{$m}A.{$m}M)/sg)
;
#AdventOfCode #perl
=> More informations about this toot | View the thread
Just for fun, here's a solution for day 3 of AoC written in standard C without using regexes:
[#]include <inttypes.h>
[#]include <stdbool.h>
[#]include <stdint.h>
[#]include <stdio.h>
[#]include <string.h>
typedef uint_least32_t u32x;
static u32x parse_uint(char **pp, const char *base) {
u32x n = 0, e = 1;
for (; *pp > base && isdigit((unsigned char)(*pp)[-1]); (*pp)--) {
n += ((*pp)[-1] - '0') * e;
e *= 10;
}
return n;
}
int main(void) {
u32x total = 0;
bool enabled = true;
char buf[4096];
while (fgets(buf, sizeof buf, stdin)) {
for (char *p = buf; (p = strchr(p, ')')); p++) {
if (enabled) {
if (p >= buf + 6 && strncmp(p - 6, "don't(", 6) == 0) {
enabled = false;
continue;
}
} else {
if (p >= buf + 3 && strncmp(p - 3, "do(", 3) == 0) {
enabled = true;
}
continue;
}
char *t = p;
const u32x b = parse_uint(&t, buf);
if (*t == ')' || t == buf || t[-1] != ',') continue;
t--;
const u32x a = parse_uint(&t, buf);
if (*t == ',' || t < buf + 4 || strncmp(t - 4, "mul(", 4) != 0) continue;
total += a * b;
}
}
printf("%" PRIuLEAST32 "\n", total);
}
#AdventOfCode #c
=> More informations about this toot | View the thread
@hallo Nanu, was ist denn mit https://dashboard.uberspace.de/dashboard los? Ich kriege einen 500-Fehler.
=> More informations about this toot | View the thread
@bfdi Ist so eine Negativ-Checkbox (Werbe-Opt-Out) eigentlich erlaubt?
(Gesehen im Online-Bestellformular von #lieferando.)
=> More informations about this toot | View the thread
Call me by your value
=> More informations about this toot | View the thread
TIL if you load a page from x.com without allowing scripts from twimg.com, all you get is an obscure error page ("Something went wrong, try again") with no indication what the problem is. I guess it's hard to render short text posts without letting third parties execute arbitrary code on the visitor's computer.
=> More informations about this toot | View the thread
@jerry Ever since the last upgrade, the "new notifications" badge doesn't clear anymore when I check my notifications, even after I mark all notifications as seen manually. Do you know what's up with that?
=> More informations about this toot | View the thread
Is Github forcing you to add "2FA" to your account, but you don't want to install another app? Turns out you can do TOTP 2FA from the command line.
When Github asks you to scan the QR code, look for the download link instead. Save the setup code in a file such as github_2fa.txt. Now when Github asks you for a security code, just run
to generate a 6-digit code.
[#]github #2fa #totp
=> More informations about this toot | View the thread
How did I miss this? Perl has had a /n regex flag for almost a decade (added in 5.22.0), which makes parentheses do the right thing. Which is grouping things without capturing, as in math. Capturing is explicit under /n.
[#]perl #regex
=> More informations about this toot | View the thread
null = \case(:){}->False;[]{}->True
#haskell
=> More informations about this toot | View the thread
Acyclic paint
=> More informations about this toot | View the thread
x && y | x = y | True = False
#haskell #logic
=> More informations about this toot | View the thread
Another stealth benefit of upgrading to the latest version of Perl: Better detection of common errors through improved diagnostics.
https://bz.apache.org/SpamAssassin/show_bug.cgi?id=8228
[#]perl #warnings
=> More informations about this toot | View the thread
[#]poll
=> More informations about this toot | View the thread
Perl 5.40.0 is out. 🎉
https://metacpan.org/release/HAARG/perl-5.40.0/view/pod/perldelta.pod
[#]perl
=> More informations about this toot | View the thread
There are 10 types of people: binary and non-binary
=> More informations about this toot | View the thread
Is there a good introduction to the ActiviityPub protocol, particularly the stuff used by Mastodon? Like, if I wanted to write my own Mastodon-like software from scratch, what should I read to get started?
[#]ActivityPub #Fediverse
=> More informations about this toot | View the thread
=> This profile with reblog | Go to barubary@infosec.exchange account This content has been proxied by September (ba2dc).Proxy Information
text/gemini