Snippets

cia_rana 7セグパネルがよく見えない 横へな2016.9.3 問題

Created by cia_rana last modified
# http://nabetani.sakura.ne.jp/hena/orde07_7seg/

LIGHT = %w!3f 06 5b 4f 66 6d 7d 27 7f 6f!.map(&:hex)

def solve l, u
  # 7セグパネルのビットが全て立っていないときは,注目桁の最小値に空白を挿入するが
  # 注目桁より上位の桁の最小値の中に空白でない桁が存在するときfalseとなるフラグ
  flg = true

  a = l.zip(u).map.with_index{|(a,b), i|
    c = ~a&~b # red parts
    d = LIGHT.map.with_index { |e, j|
      j if (a|c)&e == e && (a|e)^e == 0
    }.compact
  
    if i == 0 && l.size != 1 && !d.empty? && d[0] == 0
      if d.size == 1
        next nil
      else
        d.shift
      end
    end
  
    if flg && a == 0 && l.size - 1 != i
      d.unshift("")
    else
      flg = false
    end
    d unless d.empty?
  }
  
  # 最小値,最大値を作る処理
  if a.include?(nil)
    return ?-
  else
    0.upto(a.size-2) do |i|
      if a[i] == ""
        next
      elsif a[i][0] != 0
        break
      else
        a[i-1].shift
        if a[i-1][0] == 0
          if a[i-1].size == 1
            return ?-
          else
            a[i-1].shift
            break
          end
        end
        break
      end
    end
  end
  return a.map{|e|[e[0],e[-1]]}.transpose.map(&:join)*?,
end

puts solve *gets.split(?,).map{|e|e.split(?:).map(&:hex)}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.