blob: 50cd196d4dd7c1fc316f2a6cbcc19f19be5bf0a3 (
plain)
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
|
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)
|