day 21 part 2
This commit is contained in:
parent
d7b6edbd62
commit
7f9f94aa51
@ -7,14 +7,14 @@ local snd = tonumber(string.match(f:read("*l"), "Player 2 starting position: (.*
|
|||||||
f:close()
|
f:close()
|
||||||
|
|
||||||
local function index(score1, score2, loc1, loc2)
|
local function index(score1, score2, loc1, loc2)
|
||||||
return loc2 + 10 * (loc1 + 30 * (score2 + 30 * score1))
|
return loc2 + 10 * (loc1 + 31 * (score2 + 31 * score1))
|
||||||
end
|
end
|
||||||
|
|
||||||
local function deindex(index)
|
local function deindex(index)
|
||||||
local lo2, loc1, score2, score1
|
local lo2, loc1, score2, score1
|
||||||
loc2, index = index % 10, math.floor(index / 10)
|
loc2, index = index % 10, math.floor(index / 10)
|
||||||
loc1, index = index % 30, math.floor(index / 30)
|
loc1, index = index % 31, math.floor(index / 31)
|
||||||
score2, index = index % 30, math.floor(index / 30)
|
score2, index = index % 31, math.floor(index / 31)
|
||||||
score1 = index
|
score1 = index
|
||||||
return score1, score2, loc1, loc2
|
return score1, score2, loc1, loc2
|
||||||
end
|
end
|
||||||
@ -23,26 +23,24 @@ local poss = { [index(0, 0, fst, snd)] = 1 }
|
|||||||
local size = 1
|
local size = 1
|
||||||
local c1, c2 = 0, 0
|
local c1, c2 = 0, 0
|
||||||
while size > 0 do
|
while size > 0 do
|
||||||
print(size)
|
|
||||||
local newposs = {}
|
local newposs = {}
|
||||||
size = 0
|
size = 0
|
||||||
for i, c in pairs(poss) do
|
for i, c in pairs(poss) do
|
||||||
local s1, s2, l1, l2 = deindex(i)
|
local s1, s2, l1, l2 = deindex(i)
|
||||||
print(s1, s2, l1, l2, c)
|
|
||||||
if s2 >= 21 then
|
if s2 >= 21 then
|
||||||
c2 = c2 + c
|
c2 = c2 + c
|
||||||
else
|
else
|
||||||
local i3 = index(s2, s1 + 3, l2, (l1 + 3) % 10); newposs[i3] = (newposs[i3] or 0) + 1 * c
|
local i3 = index(s2, s1 + (l1 + 3) % 10 + 1, l2, (l1 + 3) % 10); newposs[i3] = (newposs[i3] or 0) + 1 * c
|
||||||
local i4 = index(s2, s1 + 4, l2, (l1 + 4) % 10); newposs[i4] = (newposs[i4] or 0) + 3 * c
|
local i4 = index(s2, s1 + (l1 + 4) % 10 + 1, l2, (l1 + 4) % 10); newposs[i4] = (newposs[i4] or 0) + 3 * c
|
||||||
local i5 = index(s2, s1 + 5, l2, (l1 + 5) % 10); newposs[i5] = (newposs[i5] or 0) + 6 * c
|
local i5 = index(s2, s1 + (l1 + 5) % 10 + 1, l2, (l1 + 5) % 10); newposs[i5] = (newposs[i5] or 0) + 6 * c
|
||||||
local i6 = index(s2, s1 + 6, l2, (l1 + 6) % 10); newposs[i6] = (newposs[i6] or 0) + 7 * c
|
local i6 = index(s2, s1 + (l1 + 6) % 10 + 1, l2, (l1 + 6) % 10); newposs[i6] = (newposs[i6] or 0) + 7 * c
|
||||||
local i7 = index(s2, s1 + 7, l2, (l1 + 7) % 10); newposs[i7] = (newposs[i7] or 0) + 6 * c
|
local i7 = index(s2, s1 + (l1 + 7) % 10 + 1, l2, (l1 + 7) % 10); newposs[i7] = (newposs[i7] or 0) + 6 * c
|
||||||
local i8 = index(s2, s1 + 8, l2, (l1 + 8) % 10); newposs[i8] = (newposs[i8] or 0) + 3 * c
|
local i8 = index(s2, s1 + (l1 + 8) % 10 + 1, l2, (l1 + 8) % 10); newposs[i8] = (newposs[i8] or 0) + 3 * c
|
||||||
local i9 = index(s2, s1 + 9, l2, (l1 + 9) % 10); newposs[i9] = (newposs[i9] or 0) + 1 * c
|
local i9 = index(s2, s1 + (l1 + 9) % 10 + 1, l2, (l1 + 9) % 10); newposs[i9] = (newposs[i9] or 0) + 1 * c
|
||||||
size = size + 27 * c
|
size = size + 27 * c
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
poss = newposs
|
poss = newposs
|
||||||
c1, c2 = c2, c1
|
c1, c2 = c2, c1
|
||||||
end
|
end
|
||||||
print(c1, c2)
|
io.write(string.format("max(%d %d) = %d\n", c1, c2, math.max(c1, c2)))
|
||||||
|
Loading…
Reference in New Issue
Block a user