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