feat: add puzzle selection and execution menu
This commit is contained in:
@@ -102,6 +102,9 @@ end
|
||||
local function main()
|
||||
local stats = loadStats("aoc/res/stats.json")
|
||||
local selectedDay = math.max(1, math.min(END_DATE.day, today.day))
|
||||
if not dates.isInDateRange(START_DATE, today, END_DATE) then
|
||||
selectedDay = 1
|
||||
end
|
||||
|
||||
while true do
|
||||
term.clear()
|
||||
|
||||
@@ -118,24 +118,78 @@ function Day:createFiles()
|
||||
end
|
||||
end
|
||||
|
||||
---Displays this day and prompts the user with possible actions
|
||||
function Day:show()
|
||||
function Day:getExampleData(suffix)
|
||||
return utils.readFile(self:examplePath(suffix))
|
||||
end
|
||||
|
||||
function Day:getInputData()
|
||||
return utils.readFile(self:inputPath())
|
||||
end
|
||||
|
||||
function Day:execPuzzle(puzzleI, data)
|
||||
local puzzle = require(self:srcDir() .. "/puzzle" .. puzzleI)
|
||||
local t0 = os.epoch("local")
|
||||
local result = puzzle.solve(data)
|
||||
local t1 = os.epoch("local")
|
||||
print(("(Executed in %.3fs)"):format((t1 - t0) / 1000))
|
||||
print("Result:", result)
|
||||
end
|
||||
|
||||
function Day:execExample(puzzleI, suffix)
|
||||
local data = self:getExampleData(suffix)
|
||||
return self:execPuzzle(puzzleI, data)
|
||||
end
|
||||
|
||||
function Day:execReal(puzzleI)
|
||||
local data = self:getInputData()
|
||||
return self:execPuzzle(puzzleI, data)
|
||||
end
|
||||
|
||||
function Day:choosePuzzle()
|
||||
self:printTitle()
|
||||
local c = utils.promptChoices({"Puzzle 1", "Puzzle 2", "Back"})
|
||||
return c
|
||||
end
|
||||
|
||||
function Day:printTitle()
|
||||
term.clear()
|
||||
term.setCursorPos(1, 1)
|
||||
term.setTextColor(colors.green)
|
||||
print(" -*- [ " .. self:getTitle() .. " ] -*-")
|
||||
term.setTextColor(colors.white)
|
||||
end
|
||||
|
||||
---Displays this day and prompts the user with possible actions
|
||||
function Day:show()
|
||||
while true do
|
||||
self:printTitle()
|
||||
if fs.exists(self:srcDir()) then
|
||||
local c = utils.promptChoices({CHOICES.example, CHOICES.real, CHOICES.main})
|
||||
if c == CHOICES.main then
|
||||
return
|
||||
end
|
||||
local puzzle = self:choosePuzzle()
|
||||
if puzzle ~= "Back" then
|
||||
local puzzleI = ({
|
||||
["Puzzle 1"] = 1,
|
||||
["Puzzle 2"] = 2
|
||||
})[puzzle]
|
||||
if c == CHOICES.example then
|
||||
self:execExample(puzzleI)
|
||||
elseif c == CHOICES.real then
|
||||
self:execReal(puzzleI)
|
||||
end
|
||||
utils.waitForKey(keys.enter)
|
||||
end
|
||||
else
|
||||
local c = utils.promptChoices({CHOICES.create, CHOICES.main})
|
||||
if c == CHOICES.create then
|
||||
if c == CHOICES.main then
|
||||
return
|
||||
elseif c == CHOICES.create then
|
||||
self:createFiles()
|
||||
end
|
||||
end
|
||||
print("Puzzle 1")
|
||||
end
|
||||
end
|
||||
|
||||
days.Day = Day
|
||||
|
||||
@@ -22,7 +22,37 @@ function utils.promptChoices(choices)
|
||||
break
|
||||
end
|
||||
end
|
||||
term.setTextColor(colors.white)
|
||||
return choices[c]
|
||||
end
|
||||
|
||||
function utils.readFile(path)
|
||||
if not fs.exists(path) then
|
||||
printError("File " .. path .. " not found")
|
||||
return nil
|
||||
end
|
||||
local file = fs.open(path, "r")
|
||||
if not file then
|
||||
printError("Could not open file")
|
||||
return nil
|
||||
end
|
||||
local data = file.readAll()
|
||||
file.close()
|
||||
return data
|
||||
end
|
||||
|
||||
function utils.waitForKey(targetKey)
|
||||
if targetKey then
|
||||
print("Press " .. keys.getName(targetKey):upper() .. " to continue")
|
||||
else
|
||||
print("Press any key to continue")
|
||||
end
|
||||
while true do
|
||||
local event, key, is_held = os.pullEvent("key")
|
||||
if not targetKey or key == targetKey then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return utils
|
||||
|
||||
Reference in New Issue
Block a user