From: Jan "Yenya" Kasprzak Date: Mon, 23 Dec 2024 05:48:29 +0000 (+0100) Subject: Day 23: strongly connected graph components X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=e662a2b0d9a7a037b9911d83477039be4583fda2;p=aoc.git Day 23: strongly connected graph components --- diff --git a/2024/45.pl b/2024/45.pl new file mode 100755 index 0000000..97a4351 --- /dev/null +++ b/2024/45.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl -w + +use v5.40; + +my %conn; +while (<>) { + my ($x, $y) = /\w+/g; + $conn{$x}{$y}++; + $conn{$y}{$x}++; +} + +my $count; +for my $x (keys %conn) { + for my $y (keys $conn{$x}->%*) { + for my $z (keys $conn{$x}->%*) { + next if $z eq $y; + next if !$conn{$z}{$y}; + $count++ if $x =~ /^t/ || $y =~ /^t/ || $z =~ /^t/; + } + } +} +say $count / 6; diff --git a/2024/46.pl b/2024/46.pl new file mode 100755 index 0000000..7310847 --- /dev/null +++ b/2024/46.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl -w + +use v5.40; + +my %conn; +while (<>) { + my ($x, $y) = /\w+/g; + $conn{$x}{$y}++; + $conn{$y}{$x}++; +} + +sub walk { + my ($set, @todo) = @_; + my @max = @$set; + X: + while (my $x = shift @todo) { + for my $y (@$set) { + next X if !$conn{$x}{$y}; + } + my @nset = walk([ @$set, $x ], @todo); + @max = @nset + if @nset > @max; + } + return @max; +} +say join(',', walk([], sort keys %conn)); +