diff options
author | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2020-12-25 15:51:05 +0100 |
---|---|---|
committer | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2020-12-25 15:51:05 +0100 |
commit | d4b47d15a1a837c777c1f155d24d7c3ae111a7c5 (patch) | |
tree | 633f2e6b2242cb826e18ffb30eec122d72c95442 /23/a.lua | |
parent | 0df6aa12fd433f7fefc5302f712c3060615cda0f (diff) |
Add day 23
Diffstat (limited to '23/a.lua')
-rw-r--r-- | 23/a.lua | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/23/a.lua b/23/a.lua new file mode 100644 index 0000000..811ccdc --- /dev/null +++ b/23/a.lua @@ -0,0 +1,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() |