第12回 シェル芸勉強会

一年くらい前にバッチファイルで解いたものがでてきたので、供養のため載せておきます。（コメントや解説はありません。）

上田ブログ
http://blog.ueda.asia/?p=3535

```bat:2014-08-02.bat
@echo off

set SELF=%~n0
set HELP=Usage: %SELF% NUMBER
set HOME=%~dps0
set HOME=%HOME:~0,-1%

setlocal enabledelayedexpansion
  :: 位置パラメーターを確認
  if "%~1" == "" (
    echo %HELP% >&2
    exit /b 1
  )
  :: 値が数値か確認
  (echo %~1) | findstr /r "[^0-9]." > NUL
  if %ERRORLEVEL% equ 0 (
    echo %HELP% >&2
    exit /b 1
  )
  
  :: 位置パラメーターを取得
  set n=%~1
  
  :: 値が範囲内か確認(Q1からQ8まで)
  if %n% lss 1 (
    echo %SELF%: illegal value: use 1-8 >&2
    exit /b 1
  )
  if %n% gtr 8 (
    echo %SELF%: illegal value: use 1-8 >&2
    exit /b 1
  )
  
  :: 作業フォルダーを作成
  set CWD=%HOME%\A%n%
  md %CWD%
  
  :: テストを実行
  call :A%n% %CWD%
  if %ERRORLEVEL% neq 0 (
    echo %SELF%: script abended: A%n% >&2
    exit /b 1
  )
  
  :: 作業フォルダーを削除
  cd %CWD%\..
  rd /s /q %CWD%
endlocal
goto :EOF

:A1
setlocal enabledelayedexpansion
  set CWD=%~1
  
  set i=21
  set m=1
  set n=%i%
  
  for /l %%i in (1, 1, %i%) do (
    set t=
    for /l %%j in (1, 1, %i%) do (
      if %%j equ !m! (
        set t=!t!x
      ) else if %%j equ !n! (
        set t=!t!x
      ) else (
        set t=!t! 
      )
    )
    echo !t!
    set /a m+=1
    set /a n-=1
  )
endlocal
goto :EOF

:A2
setlocal enabledelayedexpansion
  set CWD=%~1
  
  set s=たけやぶ
  
  set i=0
  set t=
  :A2.BOL
    set c=!s:~%i%,1!
  if not "%c%" == "" (
    set t=%t%%c%
    set /a i+=1
    goto A2.BOL
  )
  set /a i-=1
  
  set /a i-=1
  for /l %%i in (%i%, -1, 0) do (
    set t=!t!!s:~%%i,1!
  )
  
  echo %t%
endlocal
goto :EOF

:A3
setlocal enabledelayedexpansion
  set CWD=%~1
  
  (echo Invoke-WebRequest http://ja.wikipedia.org/wiki/%%E5%%8D%%97%%E6%%AD%%A6%%E7%%B7%%9A -OutFile %CWD%\%SELF%.tmp) | powershell -Command -
  
  start /min /wait cmd /c chcp 65001 ^& ^(set /p x="" ^< NUL ^& cmd /u /c type %CWD%\%SELF%.tmp^) ^> %CWD%\%SELF%.65001.tmp
  start /min /wait cmd /c chcp 932 ^& cmd /c type %CWD%\%SELF%.65001.tmp ^> %CWD%\%SELF%.932.tmp
  
  findstr /b "<th" %CWD%\%SELF%.932.tmp | findstr /l "style=\"width:1em;\"" > %CWD%\%SELF%.tmp
  for /f "tokens=2 delims=<>" %%i in (%CWD%\%SELF%.tmp) do (
    echo %%i
  )
  
  del %CWD%\*.tmp > NUL 2>&1
endlocal
goto :EOF

:A4
setlocal enabledelayedexpansion
  set CWD=%~1
  
  (
    echo 鹿児島県
    echo 青森県
    echo 大阪府
    echo 群馬県
  ) > %CWD%\A4.txt
  
  (echo Invoke-WebRequest http://elze.tanosii.net/d/kenmei.htm -OutFile %CWD%\%SELF%.tmp) | powershell -Command -
  
  for /f "tokens=3 delims=<>(" %%i in ('findstr /r "[都道府県]" %CWD%\%SELF%.tmp ^| find "   <TD>" ^| findstr /g:%CWD%\A4.txt') do (
    echo %%i
  )
  
  del %CWD%\A4.txt %CWD%\%SELF%.tmp > NUL 2>&1
endlocal
goto :EOF

:A5
setlocal enabledelayedexpansion
  set CWD=%~1
  
  (
    echo A 31 1234 -42 4
    echo B 10 31.1 -34 94
  ) > %CWD%\A5.txt
  
  setlocal disabledelayedexpansion
  (
    echo @if ^(0^) == ^(0^) echo off
    echo cscript.exe //nologo //e:JScript "%%~f0"
    echo goto :EOF
    echo @end
    echo var l = new Array^(^);
    echo while ^(!WScript.StdIn.AtEndOfStream^) l.push^(WScript.StdIn.ReadLine^(^)^);
    echo l.sort^(function ^(a, b^) { return parseInt^(a, 10^) - parseInt^(b, 10^); }^);
    echo for ^(var i in l^) WScript.StdOut.WriteLine^(l[i]^);
  ) > %CWD%\sort.bat
  endlocal
  
  for /f "tokens=1,*" %%m in (%CWD%\A5.txt) do (
    set t=%%m 
    copy NUL %CWD%\A5.tmp > NUL
    for %%i in (%%n) do (
      (echo %%i) >> %CWD%\A5.tmp
    )
    call %CWD%\sort.bat < %CWD%\A5.tmp > %CWD%\%SELF%.tmp
    for /f "" %%i in (%CWD%\%SELF%.tmp) do (
      set t=!t!%%i 
    )
    echo !t!
  )
  
  del %CWD%\sort.bat %CWD%\*.txt %CWD%\%SELF%.tmp > NUL 2>&1
endlocal
goto :EOF

:A6
setlocal enabledelayedexpansion
  set CWD=%~1
  
  (
    echo 5
    echo 3
    echo 4
    echo 10
    echo 2
  ) > %CWD%\A6.txt
  
  for /f "" %%i in (%CWD%\A6.txt) do (
    rem FIXME
    if %%i lss 10 (
      set i=%%i 
    ) else (
      set i=%%i
    )
    set t= !i! 
    rem FIXME
    for /l %%j in (1, 1, 20) do (
      if %%j leq %%i (
        set t=!t!*
      ) else (
        set t=!t! 
      )
    )
    echo !t!
  )
  
  del %CWD%\A6.txt > NUL 2>&1
endlocal
goto :EOF

:A7
setlocal enabledelayedexpansion
  set CWD=%~1
  
  call :A6 "%CWD%" > %CWD%\A7.txt
  
  rem FIXME
  for /l %%i in (0, 1, 20) do (
    set t=^|
    for /f "tokens=*" %%j in (%CWD%\A7.txt) do (
      set s=%%j
      set c=!s:~%%i,1! 
      if "!c!" == "" (
        set t=!t! 
      ) else (
        set t=!t! !c!
      )
    )
    echo !t!^|
  )
  
  del %CWD%\A7.txt > NUL 2>&1
endlocal
goto :EOF

:A8
setlocal enabledelayedexpansion
  set CWD=%~1
  
  (
    echo A-B 1-2
    echo B-A 3-1
    echo C-A 1-0
    echo B-C 5-4
    echo C-B 2-1
  ) > %CWD%\A8.txt
  
  copy NUL %CWD%\%SELF%.tmp > NUL
  for /f "tokens=*" %%i in (%CWD%\A8.txt) do (
    set i=%%i
    for /f "tokens=1,2,3,4 delims= " %%a in ("!i:-= !") do (
      (
      if %%c gtr %%d (
        echo %%aW
        echo %%bL
      ) else (
        echo %%aL
        echo %%bW
      )
      ) >> %CWD%\%SELF%.tmp
    )
  )
  
  sort %CWD%\%SELF%.tmp > %CWD%\%SELF%.1.tmp
  copy NUL %CWD%\%SELF%.2.tmp > NUL
  for /f "tokens=*" %%i in (%CWD%\%SELF%.1.tmp) do (
    findstr /x /c:"%%i" %CWD%\%SELF%.2.tmp > NUL || (echo %%i) >> %CWD%\%SELF%.2.tmp
  )
  
  for /f "tokens=*" %%i in (%CWD%\%SELF%.2.tmp) do (
    set t=%%i
    for /f "tokens=3" %%j in ('find /c "%%i" %CWD%\%SELF%.1.tmp') do (
      set r=!t:~1,1!
      if "!r!" == "W" (
        echo !t:~0,1! %%j勝
      ) else if "!r!" == "L" (
        echo !t:~0,1! %%j負
      ) else (
        rem /* NOT REACHED */
      )
    )
  )
  
  del %CWD%\A8.txt %CWD%\*.tmp > NUL 2>&1
endlocal
goto :EOF
```

位置パラメーターを指定せずに実行すると

```
C:\temp>2014-08-02.bat
Usage: 2014-08-02 NUMBER
```

とヘルプが表示されるので、 1 から 8 までの数字のなかから任意の値を指定して実行してください。