Skip to main content
deleted 82 characters in body
Source Link
Ell
  • 7.7k
  • 5
  • 24
  • 39

Python 2, 480 436 386 386352 bytes

exec u"""s=sorted;H=[];V=[]
FRIinput():
 S=2*map(s,zip(*R))
 FiI0,1,2,3:
    c=S[i][i/2];a,b=S[~i]
    FeIs(H):
     C,(A,B)=e
     if a<C<b&A<c<B:e[:]=C,(A,c);H+=[C,(c,B)],;V+=[c,(a,C)],;a=C
    V+=[c,(a,b)],;H,V=V,H
print sum(a==A==(d,D)&c==C==(b,B)&B-b==D-d&{d<x<D&b<y<B Fx,yI[d&1-any(od<X[0]<D&b<y<B Fy,pXIH)[::r]FrI-1,1Fp,OI[V,H][r>0]FoIO]}=={0}Fb,aIH FB,AIH Fd,cIV FD,CIV)""".translate({70:u"for ",73:u" in ",38:u" and "})
s=sorted;H=[];V=[]
for R in input():
 S=2*map(s,zip(*R))
 for i in 0,1,2,3:
    c=S[i][i/2];a,b=S[~i]
    for e in s(H):
     C,(A,B)=e
     if a<C<b and A<c<B:e[:]=C,(A,c);H+=[C,(c,B)],;V+=[c,(a,C)],;a=C
    V+=[c,(a,b)],;H,V=V,H
print sum(a==A==(d,D) and c==C==(b,B) and B-b==D-d and {d<x<D1-any(d<X[0]<D and b<y<B for x,y in [(o,p)[::r]for r in -1,1for p,O in [V,H][r>0]for oX in O]}=={0}H)for b,a in H for B,A in H for d,c in V for D,C in V)

Python 2, 480 436 386 bytes

exec u"""s=sorted;H=[];V=[]
FRIinput():
 S=2*map(s,zip(*R))
 FiI0,1,2,3:
    c=S[i][i/2];a,b=S[~i]
    FeIs(H):
     C,(A,B)=e
     if a<C<b&A<c<B:e[:]=C,(A,c);H+=[C,(c,B)],;V+=[c,(a,C)],;a=C
    V+=[c,(a,b)],;H,V=V,H
print sum(a==A==(d,D)&c==C==(b,B)&B-b==D-d&{d<x<D&b<y<B Fx,yI[(o,p)[::r]FrI-1,1Fp,OI[V,H][r>0]FoIO]}=={0}Fb,aIH FB,AIH Fd,cIV FD,CIV)""".translate({70:u"for ",73:u" in ",38:u" and "})
s=sorted;H=[];V=[]
for R in input():
 S=2*map(s,zip(*R))
 for i in 0,1,2,3:
    c=S[i][i/2];a,b=S[~i]
    for e in s(H):
     C,(A,B)=e
     if a<C<b and A<c<B:e[:]=C,(A,c);H+=[C,(c,B)],;V+=[c,(a,C)],;a=C
    V+=[c,(a,b)],;H,V=V,H
print sum(a==A==(d,D) and c==C==(b,B) and B-b==D-d and {d<x<D and b<y<B for x,y in [(o,p)[::r]for r in -1,1for p,O in [V,H][r>0]for o in O]}=={0}for b,a in H for B,A in H for d,c in V for D,C in V)

Python 2, 480 436 386 352 bytes

exec u"""s=sorted;H=[];V=[]
FRIinput():
 S=2*map(s,zip(*R))
 FiI0,1,2,3:
    c=S[i][i/2];a,b=S[~i]
    FeIs(H):
     C,(A,B)=e
     if a<C<b&A<c<B:e[:]=C,(A,c);H+=[C,(c,B)],;V+=[c,(a,C)],;a=C
    V+=[c,(a,b)],;H,V=V,H
print sum(a==A==(d,D)&c==C==(b,B)&B-b==D-d&1-any(d<X[0]<D&b<y<B Fy,XIH)Fb,aIH FB,AIH Fd,cIV FD,CIV)""".translate({70:u"for ",73:u" in ",38:u" and "})
s=sorted;H=[];V=[]
for R in input():
 S=2*map(s,zip(*R))
 for i in 0,1,2,3:
    c=S[i][i/2];a,b=S[~i]
    for e in s(H):
     C,(A,B)=e
     if a<C<b and A<c<B:e[:]=C,(A,c);H+=[C,(c,B)],;V+=[c,(a,C)],;a=C
    V+=[c,(a,b)],;H,V=V,H
print sum(a==A==(d,D) and c==C==(b,B) and B-b==D-d and 1-any(d<X[0]<D and b<y<B for y,X in H)for b,a in H for B,A in H for d,c in V for D,C in V)
added 514 characters in body
Source Link
Ell
  • 7.7k
  • 5
  • 24
  • 39

Python 2, 480 436 436386 bytes

H=[];V=[]exec u"""s=sorted;H=[];V=[]
defFRIinput():
 ES=2*map(S,Ts,czip(*R))
 FiI0,a1,b2,i=0)3:
 try   c=S[i][i/2];a,b=S[~i]
    FeIs(H):
     C,(A,B),C=S[i]=e
     if(a<C<b)*(A<c<B) a<C<b&A<c<B:S[i]=e[:]=C,(A,c);H+=[C,C;S+=((c,B)],C);V+=[c,;E(S,T,c,a,C,i)],;a=C
    E(S,T,cV+=[c,(a,b,i+1)],;H,V=V,H
print except:T+=sum(a==A==(ad,D)&c==C==(b,B)&B-b==D-d&{d<x<D&b<y<B Fx,cyI[(o,p)[::r]FrI-1,1Fp,OI[V,H][r>0]FoIO]}=={0}Fb,aIH FB,AIH Fd,cIV FD,CIV)""".translate({70:u"for ",73:u" in ",38:u" and "})

Takes a list of coordinate pairs through STDIN in the format:

[  [(x, y), (x, y)],  [(x, y), (x, y)],  ...  ]

and prints the result to STDOUT.


The actual program, after string replacement, is:
s=sorted;H=[];V=[]
for R in input():h,v=map
 S=2*map(sorteds,zip(*R));[(E
 for i in 0,1,2,3:
    c=S[i][i/2];a,b=S[~i]
    for e in s(H,V):
     C,h[i](A,*vB)=e
     if a<C<b and A<c<B:e[:]=C,E(VA,Hc);H+=[C,v[i](c,*hB)],;V+=[c,(a,C)for],;a=C
 i in 0 V+=[c,1](a,b)],;H,V=V,H
print sum((a==A==(d,D))*( and c==C==(b,B))*( and B-b==D-d) and 1-any(({d<x<D)*( and b<y<B) for x,y in{ [(o,p)[::r]for r in(1, -1)for O,1for p,O in[Vin [V,H][r>0]for o in OO]})=={0}for a,b,a in H for A,B,A in H for c,d,c in V for C,D,C in V)

Takes a list of coordinate pairs through STDIN in the format:

[  [(x, y), (x, y)],  [(x, y), (x, y)],  ...  ]

and prints the result to STDOUT.

Python 2, 480 436 bytes

H=[];V=[]
def E(S,T,c,a,b,i=0):
 try:
    (A,B),C=S[i]
    if(a<C<b)*(A<c<B):S[i]=(A,c),C;S+=((c,B),C),;E(S,T,c,a,C,i);a=C
    E(S,T,c,a,b,i+1)
 except:T+=((a,b),c),
for R in input():h,v=map(sorted,zip(*R));[(E(H,V,h[i],*v),E(V,H,v[i],*h))for i in 0,1]
print sum((a==A==(d,D))*(c==C==(b,B))*(B-b==D-d)and 1-any((d<x<D)*(b<y<B)for x,y in{(o,p)[::r]for r in(1,-1)for O,p in[V,H][r>0]for o in O})for a,b in H for A,B in H for c,d in V for C,D in V)

Takes a list of coordinate pairs through STDIN in the format:

[  [(x, y), (x, y)],  [(x, y), (x, y)],  ...  ]

and prints the result to STDOUT.

Python 2, 480 436 386 bytes

exec u"""s=sorted;H=[];V=[]
FRIinput():
 S=2*map(s,zip(*R))
 FiI0,1,2,3:
    c=S[i][i/2];a,b=S[~i]
    FeIs(H):
     C,(A,B)=e
     if a<C<b&A<c<B:e[:]=C,(A,c);H+=[C,(c,B)],;V+=[c,(a,C)],;a=C
    V+=[c,(a,b)],;H,V=V,H
print sum(a==A==(d,D)&c==C==(b,B)&B-b==D-d&{d<x<D&b<y<B Fx,yI[(o,p)[::r]FrI-1,1Fp,OI[V,H][r>0]FoIO]}=={0}Fb,aIH FB,AIH Fd,cIV FD,CIV)""".translate({70:u"for ",73:u" in ",38:u" and "})

Takes a list of coordinate pairs through STDIN in the format:

[  [(x, y), (x, y)],  [(x, y), (x, y)],  ...  ]

and prints the result to STDOUT.


The actual program, after string replacement, is:
s=sorted;H=[];V=[]
for R in input():
 S=2*map(s,zip(*R))
 for i in 0,1,2,3:
    c=S[i][i/2];a,b=S[~i]
    for e in s(H):
     C,(A,B)=e
     if a<C<b and A<c<B:e[:]=C,(A,c);H+=[C,(c,B)],;V+=[c,(a,C)],;a=C
    V+=[c,(a,b)],;H,V=V,H
print sum(a==A==(d,D) and c==C==(b,B) and B-b==D-d and {d<x<D and b<y<B for x,y in [(o,p)[::r]for r in -1,1for p,O in [V,H][r>0]for o in O]}=={0}for b,a in H for B,A in H for d,c in V for D,C in V)
edited body
Source Link
Ell
  • 7.7k
  • 5
  • 24
  • 39

Python 2, 480 440436 bytes

s=sorted;H=[];V=[]H=[];V=[]
def E(S,T,c,a,b,i=0):
 try:
    (A,B),C=S[i]
    if(a<C<b)*(A<c<B):S[i]=(A,c),C;S+=((c,B),C),;E(S,T,c,a,C,i);a=C
    E(S,T,c,a,b,i+1)
 except:T+=((a,b),c),
for R in input():h,v=map(ssorted,zip(*R));[(E(H,V,h[i],*v),E(V,H,v[i],*h))for i in 0,1]
print sum((a==A==(d,D))*(c==C==(b,B))*(B-b==D-d)and 1-any((d<x<D)*(b<y<B)for x,y in{(ao,cp)[::r]for r in(1,-1)for AO,cp in[V,H][r>0]for ao in AO})for a,b in H for A,B in H for c,d in V for C,D in V)

Takes a list of coordinate pairs through STDIN in the format:

[  [(x, y), (x, y)],  [(x, y), (x, y)],  ...  ]

and prints the result to STDOUT.

Explanation

Instead of fiddling with complex polygons, this program deals with simple line segments. For each input rectangle, we add each of its four edges to a collective segment list, individually. Adding a segment to the list goes as follows: we test each of the existing segments for intersection with the new segment; if we find an intersection, we divide both segments at the point of intersection and continue. To make things easier, we actually keep two separate segment lists: a horizonal one and a vertical one. Since segments don't overlap, horizontal segments can only intersect vertical segments and vice versa. Better yet, it means that all intersections (not considering the edges of the same rectangle) are "proper," i.e., we don't have T-shaped intersections, so "both sides" of each segment are truly divided.

Once we've constructed the segment list(s), we start counting squares. For each combination of four segments (particularly, two horizontal segments and two vertical ones,) we test if they form a square. Furthermore, we verify that no vertex lies within this square (which can happen if, for example, we have a small square inside a bigger one.) This gives us the desired quantity. Note that even though the program tests each combination four times in different orders, the particular ordering of the segment coordinates guarantees that we count each square only once.

Python 2, 480 440 bytes

s=sorted;H=[];V=[]
def E(S,T,c,a,b,i=0):
 try:
    (A,B),C=S[i]
    if(a<C<b)*(A<c<B):S[i]=(A,c),C;S+=((c,B),C),;E(S,T,c,a,C,i);a=C
    E(S,T,c,a,b,i+1)
 except:T+=((a,b),c),
for R in input():h,v=map(s,zip(*R));[(E(H,V,h[i],*v),E(V,H,v[i],*h))for i in 0,1]
print sum((a==A==(d,D))*(c==C==(b,B))*(B-b==D-d)and 1-any((d<x<D)*(b<y<B)for x,y in{(a,c)[::r]for r in(1,-1)for A,c in[V,H][r>0]for a in A})for a,b in H for A,B in H for c,d in V for C,D in V)

Takes a list of coordinate pairs through STDIN in the format:

[  [(x, y), (x, y)],  [(x, y), (x, y)],  ...  ]

and prints the result to STDOUT.

Explanation

Instead of fiddling with complex polygons, this program deals with simple line segments. For each input rectangle, we add each of its four edges to a collective segment list, individually. Adding a segment to the list goes as follows: we test each of the existing segments for intersection with the new segment; if we find an intersection, we divide both segments at the point of intersection and continue. To make things easier, we actually keep two separate segment lists: a horizonal one and a vertical one. Since segments don't overlap, horizontal segments can only intersect vertical segments and vice versa. Better yet, it means that all intersections (not considering the edges of the same rectangle) are "proper," i.e., we don't have T-shaped intersections, so "both sides" of each segment are truly divided.

Once we've constructed the segment list(s), we start counting squares. For each combination of four segments (particularly, two horizontal segments and two vertical ones,) we test if they form a square. Furthermore, we verify that no vertex lies within this square (which can happen if, for example, we have a small square inside a bigger one.) This gives us the desired quantity. Note that even though the program tests each combination four times in different orders, the particular ordering of the segment coordinates guarantees that we count each square only once.

Python 2, 480 436 bytes

H=[];V=[]
def E(S,T,c,a,b,i=0):
 try:
    (A,B),C=S[i]
    if(a<C<b)*(A<c<B):S[i]=(A,c),C;S+=((c,B),C),;E(S,T,c,a,C,i);a=C
    E(S,T,c,a,b,i+1)
 except:T+=((a,b),c),
for R in input():h,v=map(sorted,zip(*R));[(E(H,V,h[i],*v),E(V,H,v[i],*h))for i in 0,1]
print sum((a==A==(d,D))*(c==C==(b,B))*(B-b==D-d)and 1-any((d<x<D)*(b<y<B)for x,y in{(o,p)[::r]for r in(1,-1)for O,p in[V,H][r>0]for o in O})for a,b in H for A,B in H for c,d in V for C,D in V)

Takes a list of coordinate pairs through STDIN in the format:

[  [(x, y), (x, y)],  [(x, y), (x, y)],  ...  ]

and prints the result to STDOUT.

Explanation

Instead of fiddling with complex polygons, this program deals with simple line segments. For each input rectangle, we add each of its four edges to a collective segment list, individually. Adding a segment to the list goes as follows: we test each of the existing segments for intersection with the new segment; if we find an intersection, we divide both segments at the point of intersection and continue. To make things easier, we actually keep two separate segment lists: a horizonal one and a vertical one. Since segments don't overlap, horizontal segments can only intersect vertical segments and vice versa. Better yet, it means that all intersections (not considering the edges of the same rectangle) are "proper," i.e., we don't have T-shaped intersections, so "both sides" of each segment are truly divided.

Once we've constructed the segment list(s), we start counting squares. For each combination of four segments (particularly, two horizontal segments and two vertical ones,) we test if they form a square. Furthermore, we verify that no vertex lies within this square (which can happen if, for example, we have a small square inside a bigger one.) This gives us the desired quantity. Note that even though the program tests each combination four times in different orders, the particular ordering of the segment coordinates guarantees that we count each square only once.

added 236 characters in body
Source Link
Ell
  • 7.7k
  • 5
  • 24
  • 39
Loading
added 1136 characters in body
Source Link
Ell
  • 7.7k
  • 5
  • 24
  • 39
Loading
Source Link
Ell
  • 7.7k
  • 5
  • 24
  • 39
Loading