別解：十字の壁がそそり立つ世界の中を君は螺旋状に歩く (規則性、PowerShell)


「PowerShellでも、もうちょっとキレイなコードが書けますよ。」というアピールのためだけに書いた。

元ネタ：[十字の壁がそそり立つ世界の中を君は螺旋状に歩く (規則性、PowerShell) - Qiita](http://qiita.com/cielavenir/items/d9ef9f12068e99e888f2)

# 実装

```.ps1
filter solve ($InputObject)
{
    # 入力値のパース
    $n, $e, $s, $w, $d = $InputObject -split ",|:" | %{[Int64]$_}
    $l = $n + $e + $s + $w

    # ２次方程式の係数（$bはb'）
    $a = 4
    $b = $l + 2
    $c = - ($d + 1)

    # b' の解の公式
    $x = ( -$b + [Math]::Sqrt( $b * $b - $a * $c ) ) / $a

    # k 周分引く
    $k = [Int64]([Math]::Floor($x))
    $d -= $a * $k * $k + 2 * $b * $k

    # k + 1 周目から進行
    $m = 2 * ($k + 1)
    $direction = "Error"

    filter step ($num, $direction)
    {
        $day = $_ - $num
        if ($day -ge 0) {return $day}
        Set-Variable direction $direction -Scope 1
    }

    $d |
        step $e E | step $m S | step $e W |
	    step $s S | step $m W | step $s N |
	    step $w W | step $m N | step $w E |
	    step $n N | step $m E | step $n S |
        Out-Null

    $direction
}
```


# テスト

```.ps1
filter tests
{
    filter test ($id, $in, $expect)
    {
        $out = solve $in
        if ($out -eq $expect) {return "OK"}
        throw "id:${id}, in:${in}, expect:${expect}, out:${out}"
    }

    test  0 "2,3,5,4:85" S
	test  1 "1,2,3,4:1"  E
	test  2 "1,2,3,4:2"  S
	test  3 "1,2,3,4:3"  S
	test  4 "1,2,3,4:4"  W
	test  5 "1,2,3,4:27" E
	test  6 "1,2,3,4:63" E
	test  7 "1,2,3,4:40" W
	test  8 "1,4,3,2:40" S
	test  9 "3,3,3,3:30" S
	test 10 "3,3,3,3:31" E
	test 11 "3,3,3,3:32" E
	test 12 "3,3,3,3:70" S
	test 13 "3,3,3,3:71" E
	test 14 "3,3,3,3:72" E
	test 15 "1,1,1,1:7"  N
	test 16 "1,2,1,1:7"  W
	test 17 "1,6,1,1:7"  S
	test 18 "1,8,1,1:7"  E
	test 19 "1,1,1,1:30" N
	test 20 "1,2,1,1:30" W
	test 21 "1,5,1,1:30" S
	test 22 "1,8,1,1:30" E
	test 23 "9,9,9,9:99" W
	test 24 "5,6,3,8:3"  E
	test 25 "5,8,1,1:11" W
	test 26 "2,8,1,2:18" S
	test 27 "3,2,3,1:20" N
	test 28 "3,3,8,1:28" N
	test 29 "2,5,1,2:32" E
	test 30 "2,5,1,6:33" E
	test 31 "1,2,5,7:34" N
	test 32 "3,6,5,6:36" E
	test 33 "6,2,8,1:39" S
	test 34 "3,1,2,3:41" W
	test 35 "1,1,3,4:45" W
	test 36 "1,3,1,2:46" N
	test 37 "4,4,4,4:49" W
	test 38 "3,1,4,4:55" N
	test 39 "6,6,2,1:56" W
	test 40 "3,2,1,2:59" S
	test 41 "2,7,7,1:60" S
	test 42 "3,1,1,1:63" N
	test 43 "4,6,4,1:78" E
	test 44 "7,5,3,6:79" W
	test 45 "7,8,3,1:81" E
	test 46 "3,2,5,2:82" S
	test 47 "1,1,3,4:84" N
	test 48 "7,4,1,5:88" S
	test 49 "3,6,5,3:89" S
	test 50 "1,4,2,3:92" N
	test 51 "1,3,4,5:93" W
	test 52 "2,4,8,1:94" W
	test 53 "3,6,1,7:99" S

    # 追加問題
    test 100 "1234,2345,3456,4567:978593417"          E
    test 101 "1234,2345,3456,4567:978593418"          S
    test 102 "31415,92653,58979,32384:9812336139"     W
    test 103 "31415,92653,58979,32384:9812336140"     S
    test 104 "314159,265358,979323,84626:89099331642" S
    test 105 "314159,265358,979323,84626:89099331643" W
}
```

# 実行速度

60問（追加問題含む）を合計0.2～0.3秒で解けているので、あまり文句は出ないかと思っている。

```
PS C:\demo\close_laps> Measure-Command {tests}


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 206
Ticks             : 2066318
TotalDays         : 2.39157175925926E-06
TotalHours        : 5.73977222222222E-05
TotalMinutes      : 0.00344386333333333
TotalSeconds      : 0.2066318
TotalMilliseconds : 206.6318
```