Commit e662a2b0 authored by Jan Kasprzak's avatar Jan Kasprzak
Browse files

Day 23: strongly connected graph components

parent d95cb8df
Loading
Loading
Loading
Loading

2024/45.pl

0 → 100755
+22 −0
Original line number Diff line number Diff line
#!/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;

2024/46.pl

0 → 100755
+27 −0
Original line number Diff line number Diff line
#!/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));