バッチファイルでProject Euler(34)

Problem 13


8桁ずつ区切って計算します。足し算なので簡単ですね。

@echo off

setlocal
set /a a.size = 0
for /F %%s in (%1) do (
    set /a b.size = 0
    call :make_long b %%s
    call :add_long a b
)

call :long_to_string a
echo %res_long_to_string:~0,10%
exit /b 0

:make_long
    set s=%2
    if "%s:~0,-8%" == "" (
        call :push_vector %1 %s%
        exit /b 0
    )
    set /a e = 1%s:~-8% %% 100000000
    call :push_vector %1 %e%
    call :make_long %1 %s:~0,-8%
    exit /b 0

:add_long
    set /a size1 = "%1.size"
    set /a size2 = "%2.size"
    call :max %size1% %size2%
    set /a size = %ERRORLEVEL%
    set /a %1.size = %size%
    set /a k = 0
    :loop_add_long
        set /a %1_%k% += "%2_%k%"
        set /a e = "%1_%k%"
        set /a k_1 = %k% + 1
        if %e% GTR 100000000 (
            set /a %1_%k% -= 100000000
            set /a %1_%k_1% += 1
        )
        set /a k += 1
        if %k% LSS %size% goto loop_add_long
    
    set /a e = "%1_%k%"
    if %e% GTR 0 set /a %1.size = %k% + 1
    exit /b 0

:long_to_string
    set res_long_to_string=
    set /a size = "%1.size"
    set /a k = 0
    :loop_long_to_string
        set /a e = "%1_%k%"
        set res_long_to_string=%e%%res_long_to_string%
        set /a k += 1
        if %k% LSS %size% goto :loop_long_to_string
    exit /b 0

:max
    if %1 GTR %2 exit /b %1
    exit /b %2

:push_vector
    set /a size = "%1.size"
    set /a %1_%size% = %2
    set /a %1.size += 1
    exit /b 0