# $Circ->[id][lead][index] = otherID # id is capacitor's index # lead == 0 means positive # and lead == 1 means negative # index is arbitrary (each cap may point to # several other capacitors) # otherID represents the cap's index that is # being pointed to #### # B+.............. # - . . # . C1 C2 # . . . # . . . # . ......... # . C3 # . . # ............ $Circ->[0][0][0] = 1; # Cap[0]'s positive lead connects to Cap[1] $Circ->[0][1][0] = 1; # Cap[0]'s negative lead goes to Cap[1] $Circ->[0][1][1] = 2; # Cap[0]'s negative lead goes to Cap[2] $Circ->[1][0][0] = 0; # etc. $Circ->[1][1][0] = 0; $Circ->[1][1][1] = 2; $Circ->[2][0][0] = 0; # etc. $Circ->[2][0][1] = 1; # etc. $Circ->[2][1] = (); # not pointing to anything #### # read in file, make circuit ... use Data::Dumper; # 012345678901 <--(i)--> # 0 B+.....1.... # | 1 - . # (j) 2 . . # | 3 . . # 4 .........2.. open (CIRC, "circ.txt"); $cnt = 0; $i = 0; $j = 0; $on_cap = undef; while () { # put elements of circuit into m-d array last if $_ =~ /=+/; chomp; $i = 0; for $char (split //, $_) { push @{$Circ->[$i++][$j]}, $char } $j++; } $i = 1; $j = 0; if ($Circ->[0][0][0] eq 'B' && $Circ->[1][0][0] eq '+' && $Circ->[0][1][0] eq '-') { $Circ->[0][0][0] = '.'; $Circ->[1][0][0] = '.'; $Circ->[0][1][0] = '.'; } else { die "malconfigured circuit" } $on_i = $i; $on_j = $j; &make_circ ($i,$j); sub make_circ { # count caps, make sure > 1 for $x (0..scalar @$Circ-1) { for $y (0..scalar @{$Circ->[$x]}-1) { if ($Circ->[$x][$y][0] =~ /\d/) { $cnt++ } } } goto END if $cnt <= 1; $cnt = 0; $i = $_[0]; $j = $_[1]; goto END if $ignore[$i][$j]; $looked_at[$i][$j] = 1; Iter ($i,$j); END: } sub Iter { $i = $_[0]; $j = $_[1]; $val = LookAt ($i,$j); if ($val == 0) { if ($j-1 > 0) { Iter ($i,$j-1) unless $looked_at[$i][$j-1] } if ($j+1 < scalar @{$Circ->[$i]}-1) { Iter ($i,$j+1) unless $looked_at[$i][$j+1]} if ($i-1 > 0) { Iter ($i-1,$j) unless $looked_at[$i-1][$j] } if ($i+1 < scalar @$Circ-1) { Iter ($i+1,$j) unless $looked_at[$i+1][$j] } } elsif ($val == 1) { $ignore[$i][$j] = 1; } else { for $idx (0..$#looked_at) { for (0..scalar @{$looked_at[$idx]}-1) { $looked_at[$idx][$_] = 0; } } } if (defined $on_cap) { for $id (@{$circuit->[$on_cap][0]}) { if (defined $id) { $circuit->[$id][0] = $on_cap } } # when done with Cid, replace with '.' $Circ->[$on_i][$on_j] = ['.']; $on_cap = undef; make_circ ($i,$j); } } sub LookAt { $l_i = $_[0]; $l_j = $_[1]; $cell = $Circ->[$l_i][$l_j]; if ($cell->[0] eq '.') { $ret = 0 } elsif ($cell->[0] =~ /(\d)/) { if (defined $on_cap) { push @{$circuit->[$on_cap][0]}, $1; $ret = 2 } else { $on_cap = $1; $on_i = $l_i; $on_j = $l_j; $ret = 0; } } else { $ret = 1 } return $ret; } # AND THE FILE # CIRC.TXT B+.....1.... - . . . . . .........2.. ============ 1:5 2:6