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 +++++++++++++++++++++++++++++ 22/b.lua | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 22/input.txt | 53 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 22/a.lua create mode 100644 22/b.lua create mode 100644 22/input.txt (limited to '22') 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) diff --git a/22/b.lua b/22/b.lua new file mode 100644 index 0000000..ed6e862 --- /dev/null +++ b/22/b.lua @@ -0,0 +1,83 @@ +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 + +function make_signature(deck1, deck2) + return table.concat(deck1, ',') .. '#' .. table.concat(deck2, ',') +end + +function copy(deck, n) + local new_deck = {} + for i = 1, n do + table.insert(new_deck, deck[i]) + end + return new_deck +end + +function play_game(deck1, deck2, calculate_score) + local winner = nil + local previous_rounds = {} + + while not winner do + if not previous_rounds[make_signature(deck1, deck2)] then + previous_rounds[make_signature(deck1, deck2)] = true + local card1 = table.remove(deck1, 1) + local card2 = table.remove(deck2, 1) + local one_won_round = true + if card1 <= #deck1 and card2 <= #deck2 then + one_won_round = play_game(copy(deck1, card1), copy(deck2, card2), false) + else + one_won_round = card1 > card2 + end + if one_won_round then + table.insert(deck1, card1) + table.insert(deck1, card2) + else + table.insert(deck2, card2) + table.insert(deck2, card1) + end + + if #deck2 == 0 then + winner = 1 + elseif #deck1 == 0 then + winner = 2 + end + else + winner = 1 + end + end + + if calculate_score then + deck = deck1 + if winner == 2 then + deck = deck2 + end + + score = 0 + for i = 1, #deck do + score = score + i * (deck[#deck - i + 1]) + end + + return score + end + + if winner == 1 then + return true + else + return false + end +end + +print(play_game(deck1, deck2, true)) diff --git a/22/input.txt b/22/input.txt new file mode 100644 index 0000000..62dc0e5 --- /dev/null +++ b/22/input.txt @@ -0,0 +1,53 @@ +Player 1: +29 +21 +38 +30 +25 +7 +2 +36 +16 +44 +20 +12 +45 +4 +31 +34 +33 +42 +50 +14 +39 +37 +11 +43 +18 + +Player 2: +32 +24 +10 +41 +13 +3 +6 +5 +9 +8 +48 +49 +46 +17 +22 +35 +1 +19 +23 +28 +40 +26 +47 +15 +27 -- cgit v1.2.3