From 3555be54c2abb8d5ece008a60dbdfbde0ffbddd7 Mon Sep 17 00:00:00 2001 From: Alex Pickering Date: Fri, 7 Feb 2025 12:49:48 -0600 Subject: inital commit --- 07/2.lua | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 07/2.lua (limited to '07/2.lua') diff --git a/07/2.lua b/07/2.lua new file mode 100644 index 0000000..93c90df --- /dev/null +++ b/07/2.lua @@ -0,0 +1,57 @@ +local ds = {} -- unique value +local pwd, tree, output = {}, {[ds]=0}, error +local allfolders = {tree} +local function walk(dir, path, f) + local cursor = dir + for _,v in ipairs(path) do + f(cursor) + cursor = cursor[v] + end + f(cursor) + return cursor +end +local commands = {} +function commands.cd(arg) + if arg == "/" then pwd = {} + elseif arg == ".." then table.remove(pwd) + else table.insert(pwd, arg) end +end +function commands.ls() + local cursor = walk(tree, pwd, function() end) + output = function(line) + if line:match("^dir") then + local dirname = line:match("^dir (%w+)$") + cursor[dirname] = {[ds]=0} + table.insert(allfolders,cursor[dirname]) + else + local filesize, filename = line:match("(%d+) ([%w.]+)") + walk( + tree, + pwd, + function(f) f[ds] = f[ds] + tonumber(filesize) end + )[filename] = filesize + end + end + +end +for line in io.lines() do + if line:match("^%$") then + local line_split = {} + for chunk in line:gmatch("(%S+)") do + table.insert(line_split,chunk) + end + table.remove(line_split,1) -- remove '$' + -- remove command before calling it + commands[table.remove(line_split,1)](table.unpack(line_split)) + else + output(line) + end +end +local needs = 3e7 - (7e7 - tree[ds]) +table.sort(allfolders,function(a,b) return a[ds] < b[ds] end ) +for i = 1,#allfolders do + if allfolders[i][ds] > needs then + print(allfolders[i][ds]) + break + end +end -- cgit v1.2.3-70-g09d2