feat: add puzzle selection and execution menu

This commit is contained in:
2025-11-28 16:42:55 +01:00
parent 75a2b404b1
commit fcf9820f9a
3 changed files with 98 additions and 11 deletions

View File

@@ -102,6 +102,9 @@ end
local function main() local function main()
local stats = loadStats("aoc/res/stats.json") local stats = loadStats("aoc/res/stats.json")
local selectedDay = math.max(1, math.min(END_DATE.day, today.day)) 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 while true do
term.clear() term.clear()

View File

@@ -118,24 +118,78 @@ function Day:createFiles()
end end
end end
---Displays this day and prompts the user with possible actions function Day:getExampleData(suffix)
function Day:show() 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.clear()
term.setCursorPos(1, 1) term.setCursorPos(1, 1)
term.setTextColor(colors.green) term.setTextColor(colors.green)
print(" -*- [ " .. self:getTitle() .. " ] -*-") 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 if fs.exists(self:srcDir()) then
local c = utils.promptChoices({CHOICES.example, CHOICES.real, CHOICES.main}) 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 if c == CHOICES.example then
self:execExample(puzzleI)
elseif c == CHOICES.real then
self:execReal(puzzleI)
end
utils.waitForKey(keys.enter)
end end
else else
local c = utils.promptChoices({CHOICES.create, CHOICES.main}) 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() self:createFiles()
end end
end end
print("Puzzle 1") end
end end
days.Day = Day days.Day = Day

View File

@@ -22,7 +22,37 @@ function utils.promptChoices(choices)
break break
end end
end end
term.setTextColor(colors.white)
return choices[c] return choices[c]
end 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 return utils