~mcf/adventofcode

93028cbc69b00b5d943d3d28309b89026515a003 — Michael Forney 1 year, 2 days ago 8e9a09d
Tweak 9
1 files changed, 5 insertions(+), 4 deletions(-)

M 2023/9.lua
M 2023/9.lua => 2023/9.lua +5 -4
@@ 21,18 21,19 @@ In general
	P(-1) = sum i ∈ [0, n - 1] of (-1)^i       * binom(n, i + 1) * value[i]
--]]

local line, sign, coeffs, ans1, ans2 = io.read(), -1, {1}, 0, 0
local line, coeffs = io.read(), {-1}
for _ in line:gmatch('-?%d+') do
	coeffs[#coeffs + 1] = 0
	for i = #coeffs, 2, -1 do
		coeffs[i] = coeffs[i] - coeffs[i - 1]
	end
	coeffs[#coeffs + 1], sign = sign, -sign
end
local ans1, ans2 = 0, 0
repeat
	local i = 1
	for value in line:gmatch('-?%d+') do
		ans1, i = ans1 + value * coeffs[i] * sign, i + 1
		ans2    = ans2 - value * coeffs[i]
		ans1, i = ans1 + value * coeffs[#coeffs - i + 1], i + 1
		ans2    = ans2 + value * coeffs[i]
	end
	line = io.read()
until not line