Advertisement
fuxoft

Soucet stromu

Dec 29th, 2021
1,459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.69 KB | None | 0 0
  1. #!/usr/bin/env luajit
  2.  
  3. --[[*<= Version '1.1.13+D20211229T142818' =>*]]
  4.  
  5. local max_sirka = 14
  6. math.randomseed(69)
  7.  
  8. local function precti_dalsi_cislo()
  9.     return math.random(100) + 100000
  10. end
  11.  
  12. local function najdi_rodice(predchozi_radek, index, smer)
  13.     local result = {}
  14.     if not next(predchozi_radek) then
  15.         return {}
  16.     end
  17.     for _i, ind in ipairs ({index, index - smer}) do
  18.         if predchozi_radek[ind] then
  19.             table.insert(result, ind)
  20.         end
  21.     end
  22.     return result
  23. end
  24.  
  25. local sirka = 1
  26. local smer = 1
  27. local predchozi_radek = {}
  28. local cislo_radku = 0
  29. local cesta = {}
  30.  
  31. local radek
  32. repeat
  33.     cislo_radku = cislo_radku + 1
  34.     radek = {}
  35.     io.write(cislo_radku .. ": ")
  36.     for i = 1, sirka do
  37.         local cislo = precti_dalsi_cislo()
  38.         io.write(cislo.."("..i..") ")
  39.         local rodice = najdi_rodice(predchozi_radek, i, smer)
  40.         local kolik_rodicu = #rodice
  41.  
  42.         if kolik_rodicu == 0 then
  43.             polozka = {soucet = cislo}
  44.         else
  45.             local vetsi_rodic = rodice[1]
  46.             if kolik_rodicu == 2 and predchozi_radek[rodice[2]].soucet > predchozi_radek[rodice[1]].soucet then
  47.                 vetsi_rodic = rodice[2]
  48.             end
  49.             predchozi_polozka = predchozi_radek[vetsi_rodic]
  50.             polozka = {cesta = (predchozi_polozka.cesta or "").."-"..vetsi_rodic, soucet = predchozi_polozka.soucet + cislo}
  51.         end
  52.  
  53.         radek[i] = polozka
  54.     end
  55.  
  56.     if sirka == max_sirka then
  57.         smer = -1
  58.     end
  59.     sirka = sirka + smer
  60.     io.write("\n")
  61.     predchozi_radek = radek
  62. until sirka == 0
  63.  
  64. print("\nReseni: "..radek[1].cesta.."-1 (soucet: "..radek[1].soucet..")")
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement