day 21 part 2 wip
This commit is contained in:
parent
7d7581dff9
commit
d7b6edbd62
48
day21/part2.lua
Executable file
48
day21/part2.lua
Executable file
@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env luajit
|
||||
require("utils")
|
||||
|
||||
local f = io.open(arg[3])
|
||||
local fst = tonumber(string.match(f:read("*l"), "Player 1 starting position: (.*)")) - 1
|
||||
local snd = tonumber(string.match(f:read("*l"), "Player 2 starting position: (.*)")) - 1
|
||||
f:close()
|
||||
|
||||
local function index(score1, score2, loc1, loc2)
|
||||
return loc2 + 10 * (loc1 + 30 * (score2 + 30 * score1))
|
||||
end
|
||||
|
||||
local function deindex(index)
|
||||
local lo2, loc1, score2, score1
|
||||
loc2, index = index % 10, math.floor(index / 10)
|
||||
loc1, index = index % 30, math.floor(index / 30)
|
||||
score2, index = index % 30, math.floor(index / 30)
|
||||
score1 = index
|
||||
return score1, score2, loc1, loc2
|
||||
end
|
||||
|
||||
local poss = { [index(0, 0, fst, snd)] = 1 }
|
||||
local size = 1
|
||||
local c1, c2 = 0, 0
|
||||
while size > 0 do
|
||||
print(size)
|
||||
local newposs = {}
|
||||
size = 0
|
||||
for i, c in pairs(poss) do
|
||||
local s1, s2, l1, l2 = deindex(i)
|
||||
print(s1, s2, l1, l2, c)
|
||||
if s2 >= 21 then
|
||||
c2 = c2 + c
|
||||
else
|
||||
local i3 = index(s2, s1 + 3, 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 i5 = index(s2, s1 + 5, 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 i7 = index(s2, s1 + 7, 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 i9 = index(s2, s1 + 9, l2, (l1 + 9) % 10); newposs[i9] = (newposs[i9] or 0) + 1 * c
|
||||
size = size + 27 * c
|
||||
end
|
||||
end
|
||||
poss = newposs
|
||||
c1, c2 = c2, c1
|
||||
end
|
||||
print(c1, c2)
|
Loading…
Reference in New Issue
Block a user