m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--23/a.lua63
-rw-r--r--23/b.lua99
2 files changed, 162 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()
diff --git a/23/b.lua b/23/b.lua
new file mode 100644
index 0000000..6d504b0
--- /dev/null
+++ b/23/b.lua
@@ -0,0 +1,99 @@
+Node = {}
+Node.__index = Node
+function Node:new(value)
+ object = {}
+ object.value = value
+ object.right = nil
+ object.left = nil
+ return setmetatable(object, self)
+end
+
+function Node:insert(left)
+ self.left = left
+ self.right = left.right
+ self.right.left = self
+ self.left.right = self
+end
+
+function Node:detach()
+ self.left.right = self.right
+ self.right.left = self.left
+ self.left = nil
+ self.right = nil
+ return self
+end
+
+CyclicList = {}
+CyclicList.__index = CyclicList
+
+function CyclicList:new()
+ object = {}
+ object.head = nil
+ object.last = nil
+ object.index = {}
+ return setmetatable(object, self)
+end
+
+function CyclicList:insert(value)
+ node = Node:new(value)
+ self.index[value] = node
+ if self.head == nil then
+ self.head = node
+ node.left = node
+ node.right = node
+ else
+ node:insert(self.head.left)
+ end
+end
+
+function CyclicList:move_head(node)
+ self.head = node
+end
+
+input = '716892543'
+cups = CyclicList:new()
+for i = 1, #input do
+ cups:insert(tonumber(input:sub(i,i)))
+end
+for i = 10, 1000000 do
+ cups:insert(i)
+end
+
+function next_target(n)
+ n = n - 1
+ if n == 0 then
+ n = 1000000
+ end
+ return n
+end
+
+for round = 1, 10000000 do
+ current_value = cups.head.value
+
+ next_node = cups.head.right
+ next_values = {}
+ for i = 1, 3 do
+ next_values[next_node.value] = true
+ next_node = next_node.right
+ end
+
+ target = next_target(current_value)
+ while next_values[target] do
+ target = next_target(target)
+ end
+
+ target_node = cups.index[target]
+
+ for i = 1, 3 do
+ moved_node = cups.head.right:detach()
+ moved_node:insert(target_node)
+ target_node = moved_node
+ end
+
+ cups:move_head(cups.head.right)
+end
+
+one_node = cups.index[1]
+a = one_node.right.value
+b = one_node.right.right.value
+print(a * b)