diff --git a/src/lib/aoc.lua b/src/lib/aoc.lua index f0b5dd7..80eb68b 100644 --- a/src/lib/aoc.lua +++ b/src/lib/aoc.lua @@ -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() diff --git a/src/lib/days.lua b/src/lib/days.lua index 58f6172..29e2826 100644 --- a/src/lib/days.lua +++ b/src/lib/days.lua @@ -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 - if fs.exists(self:srcDir()) then - local c = utils.promptChoices({CHOICES.example, CHOICES.real, CHOICES.main}) - if c == CHOICES.example then - end - else - local c = utils.promptChoices({CHOICES.create, CHOICES.main}) - if c == CHOICES.create then - self:createFiles() +---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.main then + return + elseif c == CHOICES.create then + self:createFiles() + end end end - print("Puzzle 1") end days.Day = Day diff --git a/src/lib/utils.lua b/src/lib/utils.lua index 7a5050d..48f2eaf 100644 --- a/src/lib/utils.lua +++ b/src/lib/utils.lua @@ -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