Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env luajit
- --[[*<= Version '1.1.13+D20211229T142818' =>*]]
- local max_sirka = 14
- math.randomseed(69)
- local function precti_dalsi_cislo()
- return math.random(100) + 100000
- end
- local function najdi_rodice(predchozi_radek, index, smer)
- local result = {}
- if not next(predchozi_radek) then
- return {}
- end
- for _i, ind in ipairs ({index, index - smer}) do
- if predchozi_radek[ind] then
- table.insert(result, ind)
- end
- end
- return result
- end
- local sirka = 1
- local smer = 1
- local predchozi_radek = {}
- local cislo_radku = 0
- local cesta = {}
- local radek
- repeat
- cislo_radku = cislo_radku + 1
- radek = {}
- io.write(cislo_radku .. ": ")
- for i = 1, sirka do
- local cislo = precti_dalsi_cislo()
- io.write(cislo.."("..i..") ")
- local rodice = najdi_rodice(predchozi_radek, i, smer)
- local kolik_rodicu = #rodice
- if kolik_rodicu == 0 then
- polozka = {soucet = cislo}
- else
- local vetsi_rodic = rodice[1]
- if kolik_rodicu == 2 and predchozi_radek[rodice[2]].soucet > predchozi_radek[rodice[1]].soucet then
- vetsi_rodic = rodice[2]
- end
- predchozi_polozka = predchozi_radek[vetsi_rodic]
- polozka = {cesta = (predchozi_polozka.cesta or "").."-"..vetsi_rodic, soucet = predchozi_polozka.soucet + cislo}
- end
- radek[i] = polozka
- end
- if sirka == max_sirka then
- smer = -1
- end
- sirka = sirka + smer
- io.write("\n")
- predchozi_radek = radek
- until sirka == 0
- print("\nReseni: "..radek[1].cesta.."-1 (soucet: "..radek[1].soucet..")")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement