-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2020_day22.lua
More file actions
64 lines (60 loc) · 1.48 KB
/
Copy path2020_day22.lua
File metadata and controls
64 lines (60 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
local deck1, deck2 = {}, {}
local deck = deck1
for v in input:gmatch("[^\n]+") do
if v:match("Player 1") then
deck = deck1
elseif v:match("Player 2") then
deck = deck2
elseif tonumber(v) then
deck[#deck+1] = tonumber(v)
end
end
local copy1, copy2 = {table.unpack(deck1)}, {table.unpack(deck2)}
while #deck1 > 0 and #deck2 > 0 do
local a, b = table.remove(deck1, 1), table.remove(deck2, 1)
if a > b then
deck1[#deck1+1] = a
deck1[#deck1+1] = b
else
deck2[#deck2+1] = b
deck2[#deck2+1] = a
end
end
local deck = #deck1 == 0 and deck2 or deck1
local sum = 0
for i = 1, #deck do
sum = sum + i*deck[#deck+1-i]
end
print(sum) -- part 1
deck1, deck2 = copy1, copy2
local function play(deck1, deck2, depth)
local seen = {}
local round = 0
while #deck1 > 0 and #deck2 > 0 do
round = round + 1
local serial = table.concat(deck1,",").." | "..table.concat(deck2,",")
if seen[serial] then return true end
seen[serial] = true
local a, b = table.remove(deck1, 1), table.remove(deck2, 1)
local player1won = false
if #deck1 >= a and #deck2 >= b then
player1won = play({table.unpack(deck1, 1, a)}, {table.unpack(deck2, 1, b)}, depth+1)
else
player1won = a > b
end
if player1won then
deck1[#deck1+1] = a
deck1[#deck1+1] = b
else
deck2[#deck2+1] = b
deck2[#deck2+1] = a
end
end
local deck = #deck1 == 0 and deck2 or deck1
local sum = 0
for i = 1, #deck do
sum = sum + i*deck[#deck+1-i]
end
return deck == deck1, sum
end
print(play(deck1, deck2, 1)) -- part 2