#!/usr/bin/perl use v5.35.0; my @locks; my @keys; my $pins = -1; my $heights = 0; while (<>) { chomp; if (/^.....$/) { s/#/1/g; s/\./0/g; $pins = oct "0x$_"; $heights += $pins; } elsif (/^$/) { if ($pins) { push @locks, $heights; } else { push @keys, $heights; } $pins = -1; $heights = 0; } } unless ($pins < 0) { if ($pins) { push @locks, $heights; } else { push @keys, $heights; } } my $fits = 0; for my $lock (@locks) { for my $key (@keys) { ++$fits unless ($key + $lock) & 0x88888; } } say $fits;