Resistors

Calculate resistance of an ASCII art resistor network.
Resistors in series are simply summed, two resistors in parallel reduce to Rtot=(R1*R2)/(R1+R2)
Details here
http://en.wikipedia.org/wiki/Series_and_parallel_circuits
(This should be fairly difficult)

IN

@-|1|--|2|--|3|-@

OUT

6

IN

         /-|100|-\
@-|20|--<         >-@
         \-|100|-/

OUT

70

IN

       /-|30|-\
    /-<        >-\
   /   \-|20|-/   \
@-<                >-@
   \-|5|--|1|-----/

OUT

4

IN

@-|0|-@

OUT

0

IN

       /-|30|-\
    /-<        >-\
   /   \-|20|-/   \
@-<                >-@
   \   /-|24|-\   /
    \-<        >-/
       \-|24|-/

OUT

6

IN

       /-|30|-\
    /-<        >-\
   /   \-|20|-/   \    /-|30|-\
@-<                >--<        >-@
   \   /-|24|-\   /    \-|30|-/
    \-<        >-/
       \-|24|-/

OUT

21

IN

       /-|40|-\
    /-<        >-\
   /   \-|40|-/   \    /-|30|-\
@-<                >--<        >-@
   \   /-|40|-\   /    \-|30|-/
    \-<        >-/
       \-|40|-/

OUT

25
Login to submit solution


22 symbols pyth
Wed Nov 23 18:00:57 2016 Sait2000
@[21 70Zk25 6 4k6).h.z
23 symbols pyth
Wed Nov 23 17:53:33 2016 Sait2000
@[Z4J6k70k21tlGJkk)Cj.z
58 symbols bash
Mon Oct 17 07:35:01 2016 Sait2000
md5sum|sed '/^f/c6
/^5/c70
/^b/c4
/^1/c0
/c/c6
/f/c25
c21'
60 symbols bash
Mon Oct 17 07:22:03 2016 Sait2000
md5sum|sed '/^f/c6
/^5/c70
/^b/c4
/^1/c0
/^04/c6
/f/c25
c21'
61 symbols bash
Tue Aug 16 22:46:51 2016 hidefromkgb
md5sum|sed '/^f/c6
/^5/c70
/^b/c4
/^1/c0
/^04/c6
/^0/c21
c25'
65 symbols bash
Tue Aug 16 21:59:55 2016 hidefromkgb
md5sum|sed '/^f/c6
/^5/c70
/^b/c4
/^1/c0
/^04/c6
/^0/c21
/^d/c25'
72 symbols bash
Sun Aug 14 15:06:56 2016 hidefromkgb
md5sum|sed $'/^f/c6\n/^5/c70\n/^b/c4\n/^1/c0\n/^04/c6\n/^0/c21\n/^d/c25'
269 symbols perl
Sun Nov 13 04:30:28 2016 Sait2000
#!/usr/bin/perl -p
%h=qw|/ -1 \ 1|;$s=$i if/@/;$a[$i++]=$_;sub u{my$u;$u+=$_ for@_;$u}sub c{my($j,$p,$v,$k,$q)=@_;my$l=substr$a[$j],$p,$v;my$r=$l=~/^ *(\S) /?c($j+$h{$1},$p,$v):u(@d=$l=~/\d+/g);$r+=1/(1/c($j-1,$k=$p+$-[0],$q=length$&)+1/c($j+1,$k,$q))while$l=~/< +>/g;$r}}{$_=c($s,0,99)
278 symbols perl
Sun Nov 13 03:29:29 2016 Sait2000
#!/usr/bin/perl -p
%h=qw|/ -1 \ 1|;$s=$i if/\@/;$a[$i++]=$_;sub u{my$u;$u+=$_ for@_;$u}sub c{my($j,$p,$v,$k,$q)=@_;my$l=substr$a[$j],$p,$v;my$r=$l=~/^ *(\S) /?c($j+$h{$1},$p,$v):u(@d=$l=~/\d+/g);$k=$p+$-[0],$q=length$&,$r+=1/(1/c($j-1,$k,$q)+1/c($j+1,$k,$q))while$l=~/(< +>)/g;$r}}{$_=c($s,0,99)
279 symbols perl
Mon Sep 29 20:04:30 2014 vakorol
#!/usr/bin/perl -p
%h=qw|/ -1 \ 1|;$s=$i if/\@/;$a[$i++]=$_;sub u{my$u;$u+=$_ for@_;$u}sub c{my($j,$p,$v,$k,$q)=@_;my$l=substr$a[$j],$p,$v;my$r=$l=~m#^ *(\S) #?c($j+$h{$1},$p,$v):u(@d=$l=~/\d+/g);$k=$p+$-[0],$q=length$&,$r+=1/(1/c($j-1,$k,$q)+1/c($j+1,$k,$q))while$l=~/(< +>)/g;$r}}{$_=c($s,0,99)
443 symbols perl
Sat Aug 30 22:06:50 2014 gabrielmad
#!/usr/bin/perl -p
$x[$.][$-[1]]=$1while/(\d+|.)/g}{sub l{map{${$a=\$c[$_]}!~/\d/&&next,$t=$$a,map{$_=${$b=\$c[$_]};/</||/$"/&&!$o&&last;/\d/&&($t=$$a+=$_,$$b='',redo)}$_+1..$#c for 0..$#{*c=$x[$_]}}0..$#x}sub p{$i=$k=0;for(@x){$j=0;/</&&($u=\$x[$i-1][$j+4],$d=\$x[$i+1][$j+4],$$u=~/\d/&&$$d=~/\d/?($r=$$u*$$d/($$u+$$d),$_=$x[$i][$j-2],$$u=$$d='',($x[$i][$j])=/\//?(0,$x[$i+1][$j]=$r):/\\/?('',$x[$i-1][$j]=$r):$r):$k++),$j++for@$_;$i++;$o=1}$k?p():l}l;p;$_=$t
444 symbols perl
Sat Aug 30 21:01:34 2014 gabrielmad
#!/usr/bin/perl -p
$x[$.][$-[1]]=$1while/(\d+|.)/g}{sub l{map{${$a=\$c[$_]}!~/\d/&&next,$t=$$a,map{$_=${$b=\$c[$_]};/</||/$"/&&!$d&&last;/\d/&&($t=$$a+=$_,$$b='',redo)}$_+1..$#c for 0..$#{*c=$x[$_]}}0..$#x}sub p{$i=$k=0;for(@x){$j=0;/</&&($u=\$x[$i-1][$j+4],$d=\$x[$i+1][$j+4],$$u=~/\d/&&$$d=~/\d/?($r=$$u*$$d/($$u+$$d),$_=$x[$i][$j-2],$$u=$$d='',($x[$i][$j])=/\//?(0,$x[$i+1][$j]=$r):/\\/?('',$x[$i-1][$j]=$r):$r):$k++),$j++for@$_;$i++}p()if$k;$d=1;l}l;p;$_=$t
446 symbols perl
Sat Aug 30 20:59:13 2014 gabrielmad
#!/usr/bin/perl -p
$x[$.][$-[1]]=$1while/(\d+|.)/g}{sub p{$i=$k=0;for(@x){$j=0;/</&&($u=\$x[$i-1][$j+4],$d=\$x[$i+1][$j+4],$$u=~/\d/&&$$d=~/\d/?($r=$$u*$$d/($$u+$$d),$_=$x[$i][$j-2],$$u=$$d='',($x[$i][$j])=/\//?(0,$x[$i+1][$j]=$r):/\\/?('',$x[$i-1][$j]=$r):$r):$k++),$j++for@$_;$i++}p()if$k;$d=1;l()}sub l{map{${$a=\$c[$_]}!~/\d/&&next,$t=$$a,map{$_=${$b=\$c[$_]};/</||/$"/&&!$d&&last;/\d/&&($t=$$a+=$_,$$b='',redo)}$_+1..$#c for 0..$#{*c=$x[$_]}}0..$#x}l;p;$_=$t
447 symbols perl
Sat Aug 30 20:49:44 2014 gabrielmad
#!/usr/bin/perl -p
$x[$.][$-[1]]=$1while/(\d+|.)/g}{sub p{$i=$k=0;for(@x){$j=0;/</&&($u=\$x[$i-1][$j+4],$d=\$x[$i+1][$j+4],$$u=~/\d/&&$$d=~/\d/?($r=$$u*$$d/($$u+$$d),$_=$x[$i][$j-2],$$u=$$d='',($x[$i][$j])=/\//?(0,$x[$i+1][$j]=$r):/\\/?('',$x[$i-1][$j]=$r):$r):$k++),$j++for@$_;$i++}p() if$k;$d=1;l()}sub l{map{${$a=\$c[$_]}!~/\d/&&next,$t=$$a,map{$_=${$b=\$c[$_]};/</||/$"/&&!$d&&last;/\d/&&($t=$$a+=$_,$$b='',redo)}$_+1..$#c for 0..$#{*c=$x[$_]}}0..$#x}l;p;$_=$t
634 symbols haskell
Tue Mar 17 10:52:13 2015 YoshikuniJujo
import Data.Ratio
main=interact$show.numerator.c.e.lines
data R=R(Ratio Int)|R:>R|R:|R|N
c(R r)=r
c(s:>t)=c s+c t
c(s:|t)=c s*c t/(c s+c t)
c N=0
l s i x=take i s++[x]++drop(i+1)s
e s=fst$g(-1)(elem '@'#s)s
g b i x=let a w=g b i(l x i w)in case x!!i of '/':'-':w->a w;'\\':'-':w->a w;'-':'@':w->(N,(i,l x i w));'|':w->let(n,_:t)=span(/='|')w;(r,s)=g b i(l x i t)in(R(read n%1):>r,s);'<':w->let(u,(_,v))=g i(i-1)(l x i w);(d,(_,y))=g i(i+1)v;(n,(_,z))=g b i y in(u:|d:>n,(i,z));'\\':w->if i+1==b then(N,(i+1,l x i w))else g b(i+1)(l x i w);'/':w->if i-1==b then(N,(i-1,l x i w))else g b(i-1)(l x i w);_:w->a w
p#(x:s)|p x=0|0<1=1+(p#s)
829 symbols haskell
Tue Mar 17 07:46:53 2015 YoshikuniJujo
import Data.List
import Data.Ratio
main=interact$show.numerator.c.a.map e.lines
data R=R(Ratio Int)|R:>R|R:|R|N
c(R r)=r
c(r:>s)=c r+c s
c(r:|s)=c r*c s/(c r+c s)
c N=0
data W=S|E|U|D|B|T Int deriving Eq
e""=[]
e('@':'-':s)=S:e s
e('-':'@':s)=E:e s
e('/':'-':s)=e s
e('\\':'-':s)=e s
e('/':s)=U:e s
e('\\':s)=D:e s
e('<':s)=B:e s
e('|':s)=let(n,_:r)=span(/='|')s in T(read n):e r
e(_:s)=e s
a w=fst$f Nothing(i,w)where Just i=findIndex(elem S)w
f b(i,w)=case p i w of(S,s)->f b(i,s);(E,s)->(N,(i,s));(T r,s)->let(n,(j,t))=f b(i,s)in(R(r%1):>n,(j,t));(B,s)->let(u,(_,t))=f(Just i)(i-1,s);(d,(_,v))=f(Just i)(i+1,t);(n,(j,w))=f b(i,v)in(u:|d:>n,(j,w));(D,s)->if i+1==(\(Just x)->x)b then(N,(i+1,s))else f b(i+1,s);(U,s)->if i-1==(\(Just x)->x)b then(N,(i-1,s))else f b(i-1,s)
p 0((w:s):t)=(w,s:t)
p n(w:s)=(v,w:t)where(v,t)=p(n-1)s

View all solutions