summaryrefslogtreecommitdiff
path: root/13/1.lua
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 /13/1.lua
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 '13/1.lua')
-rw-r--r--13/1.lua43
1 files changed, 43 insertions, 0 deletions
diff --git a/13/1.lua b/13/1.lua
new file mode 100644
index 0000000..94d8bd9
--- /dev/null
+++ b/13/1.lua
@@ -0,0 +1,43 @@
+
+local lines = {}
+for line in io.lines() do table.insert(lines,line) end
+
+local sum = 0
+for i = 1,#lines,3 do
+ local l1,l2 = lines[i], lines[i+1]
+ l1 = l1:gsub("%[","{"):gsub("]","}")
+ l2 = l2:gsub("%[","{"):gsub("]","}")
+ l1 = load("return "..l1)()
+ l2 = load("return "..l2)()
+ local function compare(a,b) -- return true if a comes before b
+ assert(a ~= nil)
+ assert(b ~= nil)
+ if type(a) == "number" and type(b) == "number" then
+ if a < b then return true
+ elseif a > b then return false
+ else return nil end
+ elseif type(a) == "table" and type(b) == "table" then
+ for i = 1,math.min(#a,#b) do
+ local result = compare(a[i],b[i])
+ if result ~= nil then
+ return result
+ end
+ end
+ if #a < #b then
+ return true
+ elseif #a > #b then
+ return false
+ else
+ return nil
+ end
+ elseif type(a) == "number" and type(b) == "table" then
+ return compare({a},b)
+ elseif type(a) == "table" and type(b) == "number" then
+ return compare(a,{b})
+ end
+ end
+ if compare(l1,l2) then
+ sum = sum + ((i+2)/3)
+ end
+end
+print(sum)