Advertisement
Guest User

Untitled

a guest
Dec 25th, 2022
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.77 KB | None | 0 0
  1. local sum = 0
  2. for line in io.lines(arg[1]) do
  3.     local n = 0
  4.     local pos = 0
  5.     for i = #line, 1, -1 do
  6.         local digit = line:sub(i,i)
  7.         if digit == "-" then
  8.             n = n + -1 * (5^pos)
  9.         elseif digit == "=" then
  10.             n = n + -2 * (5^pos)
  11.         else
  12.             n = n + tonumber(digit) * (5^pos)
  13.         end
  14.         pos = pos + 1
  15.     end
  16.     sum = sum + n
  17. end
  18. sum = math.floor(sum)
  19.  
  20. local function convert(n)
  21.     local rems = {}
  22.     while n > 0 do
  23.         table.insert(rems, n % 5)
  24.         n = math.floor(n / 5)
  25.     end
  26.     return rems
  27. end
  28.  
  29. local rems = convert(sum)
  30. local ans = ""
  31. for i, n in ipairs(rems) do
  32.     if n == 0 or n == 1 or n == 2 then
  33.         ans = n .. ans
  34.     elseif n == 3 then
  35.         ans = "=" .. ans
  36.         rems[i+1] = rems[i+1] + 1
  37.     elseif n == 4 then
  38.         ans = "-" .. ans
  39.         rems[i+1] = rems[i+1] + 1
  40.     end
  41. end
  42. print(ans)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement