summaryrefslogtreecommitdiff
path: root/06
diff options
context:
space:
mode:
authorAlex Pickering <alex@cogarr.net>2025-02-07 12:49:48 -0600
committerAlex Pickering <alex@cogarr.net>2025-02-07 12:49:48 -0600
commit3555be54c2abb8d5ece008a60dbdfbde0ffbddd7 (patch)
tree278876284d07118ecdea5c48cb6453f3122887f0 /06
downloadadvent_of_code_2022-3555be54c2abb8d5ece008a60dbdfbde0ffbddd7.tar.gz
advent_of_code_2022-3555be54c2abb8d5ece008a60dbdfbde0ffbddd7.tar.bz2
advent_of_code_2022-3555be54c2abb8d5ece008a60dbdfbde0ffbddd7.zip
inital commitHEADmaster
Diffstat (limited to '06')
-rw-r--r--06/1.lua28
-rw-r--r--06/2.lua0
-rwxr-xr-x06/ext.lua62
3 files changed, 90 insertions, 0 deletions
diff --git a/06/1.lua b/06/1.lua
new file mode 100644
index 0000000..68ea3e2
--- /dev/null
+++ b/06/1.lua
@@ -0,0 +1,28 @@
+require("ext")
+local line = io.read("*a")
+
+local buf = {}
+for i = 1,14 do
+ table.insert(buf, line:sub(i,i))
+end
+function checkbuf()
+ local s,u = {},0
+ for _,v in pairs(buf) do
+ if not s[v] then
+ s[v] = true
+ u = u + 1
+ end
+ end
+ if u == 14 then
+ return true
+ end
+end
+if checkbuf() then print(14) end
+for i = 15,#line do
+ table.remove(buf,1)
+ table.insert(buf,line:sub(i,i))
+ if checkbuf() then
+ print(i)
+ break
+ end
+end
diff --git a/06/2.lua b/06/2.lua
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/06/2.lua
diff --git a/06/ext.lua b/06/ext.lua
new file mode 100755
index 0000000..c1eb1cc
--- /dev/null
+++ b/06/ext.lua
@@ -0,0 +1,62 @@
+-- Override tostring to display more info about the table
+local old_tostring = tostring
+local numtabs = 0
+local printed_tables = {}
+
+local function tostring_helper(el)
+ assert(type(el) == "table", "Tried to call helper with something that was not a table, it was a " .. type(el))
+ local mt = getmetatable(el)
+ if mt and mt.__tostring then
+ return mt.__tostring(el)
+ elseif printed_tables[el] == true then
+ return old_tostring(el)
+ else
+ printed_tables[el] = true
+ numtabs = numtabs + 1
+ local strbuilder = setmetatable({"{"},{__index = table})
+ for k,v in pairs(el) do
+ local key,value
+ if type(k) == "table" then
+ key = tostring_helper(k)
+ else
+ key = old_tostring(k)
+ end
+ if type(v) == "table" then
+ value = tostring_helper(v)
+ else
+ value = old_tostring(v)
+ end
+ strbuilder:insert(string.format("%s%s : %s", string.rep("\t",numtabs), key, value))
+ end
+ strbuilder:insert(string.rep("\t",numtabs - 1) .. "}")
+ numtabs = numtabs - 1
+ return strbuilder:concat("\n")
+ end
+
+end
+function tostring(el)
+ printed_tables = {}
+ if type(el) == "table" then
+ return tostring_helper(el)
+ end
+ return old_tostring(el)
+end
+
+-- Functions to save my hands
+function printf(fmt, ...)
+ print(string.format(fmt,...))
+end
+function errorf(fmt, ...)
+ --Our error isn't actually in this function, it's 1 above us (1) = 2
+ error(string.format(fmt,...),2)
+end
+function assertf(bool, fmt, ...)
+ assert(type(fmt) == "string", "Assertf arg #2 was \"" .. type(fmt) .. "\", expected string")
+ if not bool then
+ args = {fmt}
+ for k,v in ipairs({...}) do
+ table.insert(args,tostring(v))
+ end
+ error(string.format(unpack(args)),2)
+ end
+end