fork download
  1. =begin
  2.  
  3. 入力オプションは "a1 b1 a2 b2 a3 b3 a4 b4 a5 b5 c s"。 ex. "X Y X c1 Y c1 c1 c1 c2 c3 c4 c5"
  4.  
  5. 配線は手抜き
  6.  
  7. # NAND の SVG のオリジナルは
  8. https://j...content-available-to-author-only...a.org/wiki/NAND%E3%82%B2%E3%83%BC%E3%83%88
  9. https://j...content-available-to-author-only...a.org/wiki/NAND%E3%82%B2%E3%83%BC%E3%83%88#/media/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB:NAND_ANSI_Labelled.svg
  10.  
  11. =end
  12.  
  13. ab = gets.chomp.split
  14. exit if ab.size != 12
  15.  
  16. Logic = Struct.new( :a, :b, :q, :stg, :x, :y, :no )
  17. logics = []
  18.  
  19.  
  20. lHash = { 'x'=>0,'y'=>1, 'c1'=>2,'c2'=>3,'c3'=>4,'c4'=>5,'c5'=>6 }
  21. lName = %W{ X Y c1 c2 c3 c4 c5 }
  22. lName2 = %W{ C S }
  23. 5.times{|n|
  24. logics << Logic.new( lHash[ ab[2*n].downcase ],lHash[ ab[2*n+1].downcase ],false, false, -1,-1, n )
  25. }
  26. logics[ lHash[ ab[10].downcase ]-2 ].q = 0
  27. logics[ lHash[ ab[11].downcase ]-2 ].q = 1
  28.  
  29. # ステージ調査
  30. begin
  31. lg = []
  32. logics.each{|s|
  33. lg[2*s.no] = true if s.a <= 1
  34. lg[2*s.no+1] = true if s.b <= 1
  35. }
  36. 4.times{|st|
  37. sq = []
  38. 5.times{|i|
  39. next if logics[i].stg
  40. next unless lg[2*i] && lg[2*i+1]
  41. logics[i].stg = st
  42. sq << "c#{i+1}"
  43. }
  44. sq.each{|q|
  45. ab.each_with_index{|s,i|
  46. lg[i] = true if s == q
  47. }
  48. }
  49. }
  50. end
  51.  
  52. # 座標決定
  53. begin
  54. max_stg = []
  55. logics.each{|s| max_stg[s.stg] = (max_stg[s.stg] || 0) + 1 }
  56. max_stg.size.times{|j|
  57. si = 0
  58. logics.each_with_index{|s,i|
  59. next unless j == s.stg
  60. s.x = s.stg * 120 * 2 + 20
  61. s.y = si * 100 + 20
  62. si += 1
  63. }
  64. }
  65. (max_stg.max-1).times{|n|
  66. max_stg.each_with_index{|s,i|
  67. next unless s == n + 1
  68. logics.each{|x|
  69. next unless x.stg == i
  70. x.y += 50 * (s-n)
  71. }
  72. }
  73. }
  74. end
  75.  
  76. # SVG出力
  77. print <<_EOT_
  78. <svg xmlns:xlink="http://w...content-available-to-author-only...3.org/1999/xlink" xmlns="http://w...content-available-to-author-only...3.org/2000/svg" id="nand_logic"
  79.   width="#{120*8}"
  80.   height="#{50*8}"
  81. >
  82. <defs>
  83. <g id="nand">
  84. <path style="font-size:medium;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-indent:0;text-align:start;text-decoration:none;line-height:normal;letter-spacing:normal;word-spacing:normal;text-transform:none;direction:ltr;block-progression:tb;writing-mode:lr-tb;text-anchor:start;fill:black;fill-opacity:1;stroke:none;stroke-width:3;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate;font-family:Bitstream Vera Sans;-inkscape-font-specification:Bitstream Vera Sans"
  85. d="M 40,5 L 40,6.4285714 L 40,43.571429 L 40,45 L 41.428571,45 L 60.47619,45 C 71.744098,45 80.47619,35.999955 80.47619,25 C 80.47619,14.000045 71.744099,5.0000002 60.47619,5 C 60.47619,5 60.47619,5 41.428571,5 L 40,5 z M 42.857143,7.8571429 C 50.834264,7.8571429 55.918368,7.8571429 58.095238,7.8571429 C 59.285714,7.8571429 59.880952,7.8571429 60.178571,7.8571429 C 60.327381,7.8571429 60.409227,7.8571429 60.446429,7.8571429 C 60.465029,7.8571429 60.471543,7.8571429 60.47619,7.8571429 C 70.236853,7.857143 77.142857,15.497098 77.142857,25 C 77.142857,34.502902 69.760662,42.142857 60,42.142857 L 42.857143,42.142857 L 42.857143,7.8571429 z" />
  86. <g style="fill:none;stroke:black;stroke-width:2;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1">
  87. <path d="M 41,15 L 15,15" />
  88. <path d="M 41,35 L 15,35" />
  89. <path d="M 88.11111,25 C 101.62222,25 105,25 105,25" />
  90. <path d="M 88,25 A 4,4 0 1 1 80,25 A 4,4 0 1 1 88,25 z" />
  91. </g>
  92. </g>
  93. </defs>
  94. <style type="text/css">
  95. .wire1{ stroke:black; stroke-width:2; stroke-opacity:1; fill:none }
  96. .wire2{ stroke:blue; stroke-width:2; stroke-opacity:1; fill:none }
  97. </style>
  98.  
  99. _EOT_
  100.  
  101. logics.each{|s|
  102. puts %Q{\t<use xlink:href="#nand" x="#{s.x}" y="#{s.y}" />}
  103. }
  104. puts %Q{ <g font-weight="400" font-size="17px" >}
  105. logics.each{|s|
  106. puts %Q{\t<text text-anchor="end" x="#{s.x+38}" y="#{s.y+10}">a#{s.no+1}</text>}
  107. puts %Q{\t<text text-anchor="end" x="#{s.x+38}" y="#{s.y+32}">b#{s.no+1}</text>}
  108. puts %Q{\t<text x="#{s.x+90}" y="#{s.y+21}">c#{s.no+1}</text>}
  109. }
  110. puts " </g>"
  111.  
  112. puts %Q{ <g font-weight="bolder" font-size="20px" >}
  113. logics.each{|s|
  114. puts %Q{\t<text text-anchor="end" x="#{s.x+12}" y="#{s.y+22}">#{lName[s.a]}</text>} if s.a < 2
  115. puts %Q{\t<text text-anchor="end" x="#{s.x+12}" y="#{s.y+42}">#{lName[s.b]}</text>} if s.b < 2
  116. if s.q
  117. puts %Q{\t<path class="wire1" d="M #{s.x+105},#{s.y+25} l 20,0" />}
  118. puts %Q{\t<text x="#{s.x+112+20}" y="#{s.y+32}">#{lName2[s.q]}</text>}
  119. else
  120. sno = s.no + 2
  121. corner = s.no * 10
  122. logics.each{|s2|
  123. if s2.a == sno
  124. puts %Q{\t<path class="wire2" d="M #{s.x+105},#{s.y+25} l #{50+corner},0 l 0,#{s2.y+15-s.y-25} L #{s2.x+15},#{s2.y+15}" />}
  125. end
  126. if s2.b == sno
  127. puts %Q{\t<path class="wire2" d="M #{s.x+105},#{s.y+25} l #{50+corner},0 l 0,#{s2.y+35-s.y-25} L #{s2.x+15},#{s2.y+35}" />}
  128. end
  129. }
  130. end
  131. }
  132. print <<_EOT_
  133.   </g>
  134. </svg>
  135. _EOT_
  136.  
Success #stdin #stdout 0s 6428KB
stdin
X Y X c1 Y c1 c1 c1 c2 c3 c4 c5
stdout
<svg xmlns:xlink="http://w...content-available-to-author-only...3.org/1999/xlink" xmlns="http://w...content-available-to-author-only...3.org/2000/svg" id="nand_logic"
   width="960"
   height="400"
>
<defs>
	<g id="nand">
		<path style="font-size:medium;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-indent:0;text-align:start;text-decoration:none;line-height:normal;letter-spacing:normal;word-spacing:normal;text-transform:none;direction:ltr;block-progression:tb;writing-mode:lr-tb;text-anchor:start;fill:black;fill-opacity:1;stroke:none;stroke-width:3;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate;font-family:Bitstream Vera Sans;-inkscape-font-specification:Bitstream Vera Sans"
			d="M 40,5 L 40,6.4285714 L 40,43.571429 L 40,45 L 41.428571,45 L 60.47619,45 C 71.744098,45 80.47619,35.999955 80.47619,25 C 80.47619,14.000045 71.744099,5.0000002 60.47619,5 C 60.47619,5 60.47619,5 41.428571,5 L 40,5 z M 42.857143,7.8571429 C 50.834264,7.8571429 55.918368,7.8571429 58.095238,7.8571429 C 59.285714,7.8571429 59.880952,7.8571429 60.178571,7.8571429 C 60.327381,7.8571429 60.409227,7.8571429 60.446429,7.8571429 C 60.465029,7.8571429 60.471543,7.8571429 60.47619,7.8571429 C 70.236853,7.857143 77.142857,15.497098 77.142857,25 C 77.142857,34.502902 69.760662,42.142857 60,42.142857 L 42.857143,42.142857 L 42.857143,7.8571429 z" />
		<g style="fill:none;stroke:black;stroke-width:2;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1">
			<path d="M 41,15 L 15,15" />
			<path d="M 41,35 L 15,35" />
			<path d="M 88.11111,25 C 101.62222,25 105,25 105,25" />
			<path d="M 88,25 A 4,4 0 1 1 80,25 A 4,4 0 1 1 88,25 z" />
		</g>
	</g>
</defs>
<style type="text/css">
	.wire1{ stroke:black; stroke-width:2; stroke-opacity:1; fill:none }
	.wire2{ stroke:blue;  stroke-width:2; stroke-opacity:1; fill:none }
</style>

	<use xlink:href="#nand" x="20" y="70" />
	<use xlink:href="#nand" x="260" y="20" />
	<use xlink:href="#nand" x="260" y="120" />
	<use xlink:href="#nand" x="260" y="220" />
	<use xlink:href="#nand" x="500" y="70" />
  <g font-weight="400" font-size="17px" >
	<text text-anchor="end" x="58" y="80">a1</text>
	<text text-anchor="end" x="58" y="102">b1</text>
	<text x="110" y="91">c1</text>
	<text text-anchor="end" x="298" y="30">a2</text>
	<text text-anchor="end" x="298" y="52">b2</text>
	<text x="350" y="41">c2</text>
	<text text-anchor="end" x="298" y="130">a3</text>
	<text text-anchor="end" x="298" y="152">b3</text>
	<text x="350" y="141">c3</text>
	<text text-anchor="end" x="298" y="230">a4</text>
	<text text-anchor="end" x="298" y="252">b4</text>
	<text x="350" y="241">c4</text>
	<text text-anchor="end" x="538" y="80">a5</text>
	<text text-anchor="end" x="538" y="102">b5</text>
	<text x="590" y="91">c5</text>
  </g>
  <g font-weight="bolder" font-size="20px" >
	<text text-anchor="end" x="32" y="92">X</text>
	<text text-anchor="end" x="32" y="112">Y</text>
	<path class="wire2" d="M 125,95 l 50,0 l 0,-40 L 275,55" />
	<path class="wire2" d="M 125,95 l 50,0 l 0,60 L 275,155" />
	<path class="wire2" d="M 125,95 l 50,0 l 0,140 L 275,235" />
	<path class="wire2" d="M 125,95 l 50,0 l 0,160 L 275,255" />
	<text text-anchor="end" x="272" y="42">X</text>
	<path class="wire2" d="M 365,45 l 60,0 l 0,40 L 515,85" />
	<text text-anchor="end" x="272" y="142">Y</text>
	<path class="wire2" d="M 365,145 l 70,0 l 0,-40 L 515,105" />
	<path class="wire1" d="M 365,245 l 20,0" />
	<text x="392" y="252">C</text>
	<path class="wire1" d="M 605,95 l 20,0" />
	<text x="632" y="102">S</text>
  </g>
</svg>