fork(1) download
  1. from itertools import*
  2. E=enumerate
  3. I,P,N=Z='| -'
  4. def f(b):
  5. V=[L:='+',*L.join(N*len(b[0])),L];U=[k for i in b for k in[V,[I,*I.join(P*len(i)),I]]]+[V];q=[S:={(x,y):v for x,r in E(U)for y,v in E(r)if v in Z}];M=dict(zip([i for i in q[0]if P==q[0][i]],[j for k in b for j in k]))
  6. for c in q:
  7. T,F=[],1
  8. for x,y,o in[(x,y,[(X)for X in[(x+1,y),(x-1,y),(x,y-1),(x,y+1)]])for x,y in c if P==S[(x,y)]]:z=M[(x,y)];T+=(z>(H:=sum(P==c[O]for O in o)))*[(x,y,o)];F*=z>=H
  9. if[]==T:
  10. for X,Y in c:U[X][Y]=c[(X,Y)]
  11. return'\n'.join(map(''.join,U))
  12. if F:
  13. x,y,o=T[0]
  14. for t in combinations([(X,Y)for X,Y in o if 3<Y+2<len(U[0])and I==c[(X,Y)]or 3<X+2<len(U)and N==c[(X,Y)]],M[(x,y)]-sum(P==c[O]for O in o)):q+={**c,**{i:P for i in t}},
  15.  
  16. print(f([
  17. [0,1,0],
  18. [2,4,1],
  19. [1,1,0]
  20. ]))
  21. print('#'*30)
  22. print(f([
  23. [2,2,2,2,2],
  24. [2,1,3,1,2],
  25. [2,0,1,0,2],
  26. [2,2,2,2,2],
  27. ]))
  28. print('#'*30)
  29. print(f([
  30. [2,3,2,2],
  31. [2,1,1,1],
  32. [1,0,1,0],
  33. ]))
  34. print('#'*30)
  35. print(f([
  36. [1,1,1,1,1,1],
  37. [0,0,1,0,0,2],
  38. [0,0,0,1,2,2],
  39. ]))
Success #stdin #stdout 0.09s 14184KB
stdin
Standard input is empty
stdout
+-+-+-+
| | | |
+-+-+-+
|     |
+-+-+-+
| | | |
+-+-+-+
##############################
+-+-+-+-+-+
|   |     |
+-+-+-+-+-+
| | |   | |
+-+-+-+-+-+
| | | | | |
+-+-+-+-+-+
|         |
+-+-+-+-+-+
##############################
+-+-+-+-+
|       |
+-+-+-+-+
| | | | |
+-+-+-+-+
| | | | |
+-+-+-+-+
##############################
+-+-+-+-+-+-+
|   | |   | |
+-+-+-+-+-+-+
| | | | | | |
+-+-+-+-+-+-+
| | | |     |
+-+-+-+-+-+-+