From d4b47d15a1a837c777c1f155d24d7c3ae111a7c5 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Fri, 25 Dec 2020 15:51:05 +0100 Subject: Add day 23 --- 23/a.lua | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 23/a.lua (limited to '23/a.lua') 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() -- cgit v1.2.3