Skip to content

Commit

Permalink
Update lib.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
Pirrkatt committed Dec 9, 2023
1 parent b17f293 commit 3b82507
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ function GetInput(demo)
local path = info.source:match("Advent of Code\\(.*)part")
path = demo == "demo" and (path .. "demo-input.txt") or (path .. "input.txt")

local file = io.open(path, "r")
if not file then return nil end
local file, errorMsg = io.open(path, "r")
if not file then
error("\n-- ERROR: Unable to open file - " .. errorMsg)
end

local lines = {}
for line in file:lines() do
Expand Down Expand Up @@ -105,6 +107,16 @@ function table.contains(table, value)
return false
end

function table.occurrences(table, value)
local occurences = 0
for _, v in pairs(table) do
if v == value then
occurences = occurences + 1
end
end
return occurences
end

function table.getKey(table, value)
for k, v in pairs(table) do
if v == value then
Expand Down

0 comments on commit 3b82507

Please sign in to comment.