m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/23/a.lua
blob: 811ccdcc67b4de8fa9243940f7aa4c6e50774668 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
input = '716892543'
cups = {}
for i = 1, #input do
    table.insert(cups, tonumber(input:sub(i,i)))
end

function next_target(n)
    n = n - 1
    if n == 0 then
        n = 9
    end
    return n
end

for round = 1, 100 do
    current_value = cups[1]

    next_values = {}
    for i = 2, 4 do
        next_values[cups[i]] = true
    end

    target = next_target(current_value)
    while next_values[target] do
        target = next_target(target)
    end

    target_index = 5
    while cups[target_index] ~= target do
        target_index = target_index + 1
    end

    new_cups = {}
    if target_index ~= 5 then
        for i = 5, target_index - 1 do
            table.insert(new_cups, cups[i])
        end
    end
    table.insert(new_cups, target)
    for i = 2, 4 do
        table.insert(new_cups, cups[i])
    end
    if target_index ~= 9 then
        for i = target_index + 1, 9 do
            table.insert(new_cups, cups[i])
        end
    end
    table.insert(new_cups, cups[1])
    cups = new_cups
end

one_index = 1
while cups[one_index] ~= 1 do
    one_index = one_index + 1
end

for i = one_index + 1, 9 do
    io.write(cups[i])
end
for i = 1, one_index - 1 do
    io.write(cups[i])
end
print()