Skip to content

Instantly share code, notes, and snippets.

@sinkovsky
Created June 18, 2010 19:00
Show Gist options
  • Save sinkovsky/444057 to your computer and use it in GitHub Desktop.
Save sinkovsky/444057 to your computer and use it in GitHub Desktop.
#!perl
package Gate;
use Data::Dumper;
use warnings;
use Carp;
sub new {
my $self = bless {}, 'Gate';
$self->{input}->{L} = $_[1];
$self->{input}->{R} = $_[2];
$self->{output}->{L} = $_[3];
$self->{output}->{R} = $_[4];
$self->{number} = $_[5];
return $self;
}
my $c = '19L:
12R13R0#1R12R,
14R0L0#4R9L,
9R10R0#3L8L,
2L17R0#5L9R,
15R1L0#10R13R,
3L18R0#6L15L,
5L11R0#13L12L,
19R16R0#11R8R,
2R7R0#11L10L,
1R3R0#18L2L,
8R4L0#16L2R,
8L7L0#15R6R,
6R0R0#14L0L,
6L4R0#14R0R,
12L13L0#17L1L,
5R11L0#16R4L,
10L15L0#17R7R,
14L16L0#18R3R,
9L17L0#19R5R,
X18L0#X7L:
19L';
$c =~ s/[\n\r]//g;
my ($x_l, $circuit, $x_r) = $c =~ /(\d+[LR]):(.+?):(\d+[LR])/s;
my @nodes = map { $_ =~ tr/\n\r//; $_ } split /,/, $circuit;
my $gates = {};
my $cnt = 0;
foreach my $node (@nodes) {
my @matches = $node =~ /((?:\d+[LR])|X)((?:\d+[LR])|X)0#((?:\d+[LR])|X)((?:\d+[LR])|X)/;
my $gate = Gate->new(@matches, $cnt);
$gates->{$cnt} = $gate;
$cnt++;
}
print Dumper($gates);
foreach ( keys %$gates) {
my $gate = $gates->{$_};
foreach my $source_direction ( ('input', 'output') ) {
my $target_direction;
if ( $source_direction eq 'input' ) {
$target_direction = 'output';
} else {
$target_direction = 'input';
}
foreach my $source_side ( ('L','R') ) {
my $connection = $gate->{$source_direction}->{$source_side};
my $exchange = $connection =~ /X/;
## check exchange
unless ( $exchange ) {
my ($target_gate, $side) = $connection =~ /(\d+)([LR])/;
if ( $gates->{$target_gate}->{$target_direction}->{$side} eq $_.$source_side ) {
print "$connection verified\n";
} else {
croak "Connection $connection failed\n";
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment