feat: store result in cache dir and display in puzzle selection

This commit is contained in:
2025-11-28 21:48:44 +01:00
parent d2752a04b6
commit 7490ea5b47
3 changed files with 74 additions and 18 deletions

View File

@@ -1,5 +1,6 @@
SRC_PATH = "/aoc/src" SRC_PATH = "/aoc/src"
RES_PATH = "/aoc/res" RES_PATH = "/aoc/res"
CACHE_PATH = "/.cache/aoc"
package.path = SRC_PATH .. "/lib/?.lua;" .. package.path package.path = SRC_PATH .. "/lib/?.lua;" .. package.path

View File

@@ -2,7 +2,7 @@ local json = require("json")
local utils = require("utils") local utils = require("utils")
local days = {} local days = {}
local DAY_CACHE_PATH = "/.cache/days" local DAY_CACHE_PATH = CACHE_PATH .. "/days"
local CHOICES = { local CHOICES = {
create = "Create files", create = "Create files",
@@ -26,7 +26,8 @@ return puzzle%d
---@field title string? ---@field title string?
---@field puzzle1 boolean ---@field puzzle1 boolean
---@field puzzle2 boolean ---@field puzzle2 boolean
local Day = {day = 0, title = nil, puzzle1 = false, puzzle2 = false} ---@field results table
local Day = {day = 0, title = nil, puzzle1 = false, puzzle2 = false, results={}}
Day.__index = Day Day.__index = Day
---Creates a new Day object ---Creates a new Day object
@@ -40,9 +41,14 @@ function Day.new(dayI, puzzle1, puzzle2)
day.day = dayI day.day = dayI
day.puzzle1 = puzzle1 day.puzzle1 = puzzle1
day.puzzle2 = puzzle2 day.puzzle2 = puzzle2
day:loadResults()
return day return day
end end
function Day:cacheDir()
return DAY_CACHE_PATH .. ("/%02d"):format(self.day)
end
---Returns the title of this day. ---Returns the title of this day.
--- ---
---This function looks in the following places, in order: ---This function looks in the following places, in order:
@@ -54,7 +60,7 @@ function Day:getTitle()
if self.title then if self.title then
return self.title return self.title
end end
local cachePath = DAY_CACHE_PATH .. ("/%02d.txt"):format(self.day) local cachePath = self:cacheDir() .. "/name.txt"
if fs.exists(cachePath) then if fs.exists(cachePath) then
local cache = fs.open(cachePath, "r") local cache = fs.open(cachePath, "r")
if cache then if cache then
@@ -66,7 +72,7 @@ function Day:getTitle()
end end
end end
end end
fs.makeDir(DAY_CACHE_PATH) fs.makeDir(self:cacheDir())
local res = http.get("https://adventofcode.com/2024/day/" .. self.day) local res = http.get("https://adventofcode.com/2024/day/" .. self.day)
local title = "Day " .. self.day local title = "Day " .. self.day
if res then if res then
@@ -127,7 +133,21 @@ function Day:getInputData()
return utils.readFile(self:inputPath()) return utils.readFile(self:inputPath())
end end
function Day:execPuzzle(puzzleI, data) function Day:getResultKey(real, puzzle, suffix)
local key = ""
if real then
key = "input"
else
key = "example"
if suffix then
key = key .. "_" .. suffix
end
end
key = key .. "-" .. puzzle
return key
end
function Day:execPuzzle(puzzleI, data, resultKey)
local path = self:srcDir() .. "/puzzle" .. puzzleI local path = self:srcDir() .. "/puzzle" .. puzzleI
package.loaded[path] = nil package.loaded[path] = nil
local puzzle = require(path) local puzzle = require(path)
@@ -136,21 +156,32 @@ function Day:execPuzzle(puzzleI, data)
local t1 = os.epoch("local") local t1 = os.epoch("local")
print(("(Executed in %.3fs)"):format((t1 - t0) / 1000)) print(("(Executed in %.3fs)"):format((t1 - t0) / 1000))
print("Result:", result) print("Result:", result)
self.results[resultKey] = result
self:saveResults()
end end
function Day:execExample(puzzleI, suffix) function Day:execExample(puzzleI, suffix)
local data = self:getExampleData(suffix) local data = self:getExampleData(suffix)
return self:execPuzzle(puzzleI, data) return self:execPuzzle(puzzleI, data, self:getResultKey(false, puzzleI, suffix))
end end
function Day:execReal(puzzleI) function Day:execReal(puzzleI)
local data = self:getInputData() local data = self:getInputData()
return self:execPuzzle(puzzleI, data) return self:execPuzzle(puzzleI, data, self:getResultKey(true, puzzleI))
end end
function Day:choosePuzzle() function Day:choosePuzzle(real)
self:printTitle() self:printTitle()
local c = utils.promptChoices({"Puzzle 1", "Puzzle 2", "Back"}) local choices = {
{"Puzzle 1", 1},
{"Puzzle 2", 2},
"Back"
}
local res1 = self.results[self:getResultKey(real, 1)]
local res2 = self.results[self:getResultKey(real, 2)]
if res1 then choices[1][1] = choices[1][1] .. (" - %s"):format(res1) end
if res2 then choices[2][1] = choices[2][1] .. (" - %s"):format(res2) end
local c = utils.promptChoices(choices)
return c return c
end end
@@ -197,6 +228,26 @@ function Day:saveStats()
utils.writeFile(path, content, true) utils.writeFile(path, content, true)
end end
function Day:loadResults()
local path = self:cacheDir() .. "/results.json"
local results = {}
if fs.exists(path) then
local data = utils.readFile(path)
local r = json.loads(data)
if type(r) == "table" then
results = r
end
end
self.results = results
end
function Day:saveResults()
local data = json.dumps(self.results)
fs.makeDir(self:cacheDir())
local path = self:cacheDir() .. "/results.json"
utils.writeFile(path, data, true)
end
function Day:printTitle() function Day:printTitle()
term.clear() term.clear()
term.setCursorPos(1, 1) term.setCursorPos(1, 1)
@@ -216,16 +267,12 @@ function Day:show()
elseif c == CHOICES.star then elseif c == CHOICES.star then
self:menuStars() self:menuStars()
else else
local puzzle = self:choosePuzzle() local puzzle = self:choosePuzzle(c == CHOICES.real)
if puzzle ~= "Back" then 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) self:execExample(puzzle)
elseif c == CHOICES.real then elseif c == CHOICES.real then
self:execReal(puzzleI) self:execReal(puzzle)
end end
utils.waitForKey(keys.enter) utils.waitForKey(keys.enter)
end end

View File

@@ -11,7 +11,11 @@ function utils.promptChoices(choices, default)
else else
term.setTextColor(colors.lightGray) term.setTextColor(colors.lightGray)
end end
print(choice) local label = choice
if type(choice) == "table" then
label = choice[1] or choice["label"]
end
print(label)
end end
local event, key, is_held = os.pullEvent("key") local event, key, is_held = os.pullEvent("key")
if key == keys.up then if key == keys.up then
@@ -23,7 +27,11 @@ function utils.promptChoices(choices, default)
end end
end end
term.setTextColor(colors.white) term.setTextColor(colors.white)
return choices[c] local choice = choices[c]
if type(choice) == "table" then
choice = choice[2] or choice["value"]
end
return choice
end end
function utils.readFile(path) function utils.readFile(path)