adventofcode-2021/day07/part1.lua

15 lines
288 B
Lua
Raw Normal View History

2021-12-07 08:29:45 +01:00
#!/usr/bin/env luajit
require("utils")
local positions = split(io.open(arg[3]):read("*a"), ",")
foreach(positions, tonumber)
table.sort(positions)
local median = positions[#positions/2]
local cost = 0
for k, v in pairs(positions) do
cost = cost + math.abs(median - v)
end
print(cost)