LoginSignup
3
3

More than 5 years have passed since last update.

yhpgの「異星の電光掲示板」をSwiftで

Last updated at Posted at 2014-10-09
問題 http://nabetani.sakura.ne.jp/hena/ord15elebubo/
Ruby/C++/Python/C http://qiita.com/cielavenir/items/3d147bdeebcadcb4e3d9
Ruby(Golf,鍋谷さんのアルゴリズム復習) http://qiita.com/cielavenir/items/732a3140efe3db571480
Swift http://qiita.com/cielavenir/items/600e264d39c402b244c1

標準入力の取り方がわかったので。Cの答案を書き換えてみた。
yhpgのSwift答案は初だと思う。

コンパイル方法は http://qiita.com/cielavenir/items/abcf982d2695b357dd5b を参考に。

採点プログラムはv1(C++)とv2(Ruby)両方使える。

tyama_hena15.swift
#!/usr/bin/env xcrun -sdk macosx10.10 swift
//http://qiita.com/Nabetani/items/cba03c96d1ea55f6e861
//http://nabetani.sakura.ne.jp/hena/ord15elebubo/
import Darwin
var v:[UInt]=[0,0] // I cannot specify the array size...
var i=0,j=0
for ;(withUnsafePointer(&v[0]){vscanf("%08x",getVaList([COpaquePointer($0)]))}>0); {
    withUnsafePointer(&v[1]){vscanf("/%08x",getVaList([COpaquePointer($0)]))}
    var a:[[Int]]=[[],[]]
    for i=0;i<2;i++ {
        for j=0;j<32;j++ {
            a[i].append( Int((v[i]>>(31-j))&1) )
        }
        a[i].append(0)
        a[i].append(0)
    }

    for i=0;i<32; {
        if a[0][i] == 0 && a[1][i] == 0 {
            i++
        } else if a[0][i] != 0 && a[1][i] != 0 { //need space; otherwise treated as "a[0][i]!"
            if a[0][i+1] != 0 {
                if a[0][i+2] != 0 {
                    print("N")
                    i+=3
                } else {
                    print("R")
                    i+=2
                }
            } else if a[1][i+1] != 0 {
                if a[1][i+2] != 0 {
                    print("U")
                    i+=3
                }else{
                    print("L")
                    i+=2
                }
            }
        } else if a[0][i] != 0 {
            if a[0][i+2] != 0 {
                print("T")
                i+=3
            } else { //if a[1][i+2] != 0
                print("Z")
                i+=3
            }
        } else {
            if a[0][i+2] != 0 {
                print("S")
                i+=3
            } else {
                print("J")
                i+=2
            }
        }
    }
    println()
    fflush(stdout)
}
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3