Guest User

Untitled

a guest
Dec 6th, 2025
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.12 KB | None | 0 0
  1. package.path = package.path .. ";../lib/?.lua"
  2. local helper = require("helper")
  3. helper.load_lib("../lib/libaoc.so")
  4.  
  5.  
  6. local function apply_op(arr, op)
  7.     local ret
  8.     if op == "*" then
  9.         ret = 1
  10.         for _, n in ipairs(arr) do
  11.             ret = ret * n
  12.         end
  13.     else
  14.         ret = 0
  15.         for _, n in ipairs(arr) do
  16.             ret = ret + n
  17.         end
  18.     end
  19.     return ret
  20. end
  21.  
  22. local function part1(input)
  23.     local ret = 0
  24.     local input_lines = helper.line_iter(input)
  25.     local ops = {}
  26.     for line in input_lines do
  27.         local num_got = false
  28.         local i = 1 -- the index of the list in ops
  29.         local init = (#ops == 0)
  30.         for val in line:gmatch("%d+") do
  31.             num_got = true
  32.             if init then -- first line of numbers
  33.                 local add = { tonumber(val) }
  34.                 table.insert(ops, add)
  35.             else -- just appending
  36.                 table.insert(ops[i], tonumber(val))
  37.                 i = i + 1
  38.             end
  39.         end
  40.         if not num_got then -- regex missed so these are the operators
  41.             for op in line:gmatch("%S+") do
  42.                 ret = ret + apply_op(ops[i], op)
  43.                 i = i + 1
  44.             end
  45.         end
  46.     end
  47.     return ret
  48. end
  49.  
  50.  
  51. local function part2(input)
  52.     local ret = 0
  53.     local split_input = {}
  54.     local max_len = 0
  55.     for line in helper.line_iter(input) do
  56.         local split_line = {}
  57.         for c in line:gmatch(".") do
  58.             table.insert(split_line, c)
  59.         end
  60.         if #split_line > max_len then
  61.             max_len = #split_line
  62.         end
  63.         table.insert(split_input, split_line)
  64.     end
  65.     local nums = {}
  66.     local op = ""
  67.     for char_index = 1, max_len do
  68.         local all_empty = true
  69.         local curr_num = ""
  70.         for _, arr in ipairs(split_input) do
  71.             local c = arr[char_index]
  72.             if c == " " or c == nil then
  73.                 goto continue
  74.             end
  75.             if c == "+" or c == "*" then
  76.                 op = c
  77.             else -- c is a number
  78.                 curr_num = curr_num .. c
  79.                 all_empty = false
  80.             end
  81.             ::continue::
  82.         end
  83.         if all_empty then
  84.             for i, _ in ipairs(nums) do
  85.                 nums[i] = tonumber(nums[i])
  86.             end
  87.             ret = ret + apply_op(nums, op)
  88.             nums = {}
  89.             op = ""
  90.         else
  91.             table.insert(nums, curr_num)
  92.         end
  93.     end
  94.     return ret + apply_op(nums, op)
  95. end
  96.  
  97.  
  98. local sample = helper.get_sample("sample.txt")
  99. local input = helper.get_input(6, 2025)
  100.  
  101. print(part1(sample))
  102. print(part2(input))
  103.  
Advertisement
Add Comment
Please, Sign In to add comment