Advertisement
musifter

AoC 2024, day 23, part 1 (Perl)

Dec 23rd, 2024 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.63 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use feature         qw(say);
  7.  
  8. my %graph;
  9. foreach my $conx (map {[m/(\w{2})-(\w{2})/]} <>) {
  10.     push( $graph{$conx->[0]}->@*, $conx->[1] );
  11.     push( $graph{$conx->[1]}->@*, $conx->[0] );
  12. }
  13.  
  14. my $part1 = 0;
  15. my %table;
  16. foreach my $comp (grep {m/^t/} keys %graph) {
  17.     foreach my $neigh ($graph{$comp}->@*) {
  18.         foreach my $third ($graph{$neigh}->@*) {
  19.             next if (!grep {$_ eq $comp} $graph{$third}->@*);
  20.  
  21.             my @sort = sort( $comp, $neigh, $third );
  22.             $part1++ if ($comp ne $third and !$table{"@sort"}++);
  23.         }
  24.     }
  25. }
  26.  
  27. say "Part 1: $part1";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement