m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/22/a.lua
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2020-12-25 15:49:44 +0100
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2020-12-25 15:49:44 +0100
commit0df6aa12fd433f7fefc5302f712c3060615cda0f (patch)
tree0d876e6a86c6c7266620064abd2232ddb036f625 /22/a.lua
parent52268f4b32bb92b42d855dcc2788de45bb313a4d (diff)
Add day 22
Diffstat (limited to '22/a.lua')
-rw-r--r--22/a.lua40
1 files changed, 40 insertions, 0 deletions
diff --git a/22/a.lua b/22/a.lua
new file mode 100644
index 0000000..50cd196
--- /dev/null
+++ b/22/a.lua
@@ -0,0 +1,40 @@
+deck1 = {}
+file = io.open('input.txt')
+for line in file:lines() do
+ if line:find('%d+') then
+ table.insert(deck1, tonumber(line))
+ elseif line == '' then
+ break
+ end
+end
+deck2 = {}
+for line in file:lines() do
+ if line:find('%d+') then
+ table.insert(deck2, tonumber(line))
+ end
+end
+
+ended = false
+
+while #deck1 > 0 and #deck2 > 0 do
+ card1 = table.remove(deck1, 1)
+ card2 = table.remove(deck2, 1)
+ if card1 > card2 then
+ table.insert(deck1, card1)
+ table.insert(deck1, card2)
+ else
+ table.insert(deck2, card2)
+ table.insert(deck2, card1)
+ end
+end
+
+deck = deck1
+if #deck1 == 0 then
+ deck = deck2
+end
+
+score = 0
+for i = 1, #deck do
+ score = score + i * (deck[#deck - i + 1])
+end
+print(score)