de[v|b]log

ShellScript, Coffee, iOS/OSX Dev
Origin: Himajinworks.
About.

あけましておめでとうございます。 今年も恒例の書き初めを。

言語の選定は 1年1言語ったー で。

Screen Shot 2016-01-01 at 12.41.42

なるほど、初めての言語だ。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
class Newyear
{
    static function main()
    {
        start();
    }

    public static function start()
    {
        trace(decrypt(loadCipher()));
    }

    public static function loadCipher(): Array<Array<Int>>
    {
        var cipher: Array<Array<Int>> = [[5, 7, 37, 53, 79, 1291],
                                         [2, 2, 3, 53, 61, 622049],
                                         [2, 2, 2, 2, 2, 19, 59, 617],
                                         [3, 7, 7, 27211]];
        return cipher;
    }

    public static function decrypt(cipher: Array<Array<Int>>): String
    {
        var iter                    = cipher.iterator();
        var asciis: Array<Int>      = [];
        var mult: Int -> Int -> Int = function(lhs, rhs) { return lhs * rhs; };
        var toInt: String -> Int    = function(str) { return Std.parseInt(str); };

        while (iter.hasNext()) {
            var s        = Std.string(Lambda.fold(iter.next(), mult, 1));
            var validLen = 3 * Math.floor(s.length / 3 + 1);
            var padded   = StringTools.lpad(s, "0", validLen);
            asciis       = asciis.concat(toArray(Lambda.map(trioCharString(padded, 3), toInt)));
        }

        return toString(toAsciiDomain(asciis));
    }

    public static function trioCharString(str: String, len: Int): Array<String>
    {
        var arr: Array<String> = [];
        var chunk: String      = "";

        for (e in str.split("")) {
            chunk = chunk + e;

            if (chunk.length == len) {
                arr.push(chunk);
                chunk = "";
            }
        }

        return arr;
    }

    public static function toArray(list: List<Int>): Array<Int>
    {
        var iter            = list.iterator();
        var arr: Array<Int> = [];

        while (iter.hasNext()) {
            arr.push(iter.next());
        }

        return arr;
    }

    public static function toAsciiDomain(arr: Array<Int>): Array<Int>
    {
        var iter             = arr.iterator();
        var rarr: Array<Int> = [];
        var base             = Math.floor(Math.exp(4.17) + 1);
        var k                = 100;

        while (iter.hasNext()) {
            var v = iter.next();

            if (v < k) {
                rarr.push(v + base);
            } else {
                rarr.push(base + (-1 * (v - k)));
            }
        }

        return rarr;
    }

    public static function toString(arr: Array<Int>): String
    {
        var iter = arr.iterator();
        var str  = "";

        while (iter.hasNext()) {
            str += String.fromCharCode(iter.next());
        }

        return str;
    }
}

前書いたやつのHaxe版だけど許して。 ちゃんと動くと HAPPY NEW YEAR が出るはず。