From 3b8250715d98fcc378b8ce24bb3f6ca8b5c3697e Mon Sep 17 00:00:00 2001 From: Daniel Sehr Date: Sat, 9 Dec 2023 23:35:45 +0100 Subject: [PATCH] Update lib.lua --- lib.lua | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib.lua b/lib.lua index 2501ab6..b6dc683 100644 --- a/lib.lua +++ b/lib.lua @@ -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 @@ -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