Advertisement
Guest User

aoc22 / day 4 / lua

a guest
Dec 4th, 2022
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.56 KB | None | 0 0
  1. function checkRangesFull(range1,range2)
  2.   local r1l = ""
  3.   local r1u = ""
  4.   local r2l = ""
  5.   local r2u = ""
  6.   local r1r = 0
  7.   local r2r = 0
  8.   local delimit = false
  9.   local fullMatch = true
  10.   for c=1, #range1 do
  11.     if range1:sub(c,c) ~= "-" and not delimit then r1l = r1l..range1:sub(c,c)
  12.     elseif range1:sub(c,c) == "-" then delimit = true
  13.     elseif range1:sub(c,c) ~= "-" and delimit then r1u = r1u..range1:sub(c,c) else print("this shouldn't be running") end
  14.   end
  15.   delimit = false
  16.   for c=1, #range2 do  
  17.     if range2:sub(c,c) ~= "-" and not delimit then r2l = r2l..range2:sub(c,c)
  18.     elseif range2:sub(c,c) == "-" then delimit = true
  19.     elseif range2:sub(c,c) ~= "-" and delimit then r2u = r2u..range2:sub(c,c) else print("this shouldn't be running")  end
  20.   end
  21.   r1r = tonumber(r1u) - tonumber(r1l)
  22.   r2r = tonumber(r2u) - tonumber(r2l)
  23.   local r1t = {}
  24.   local r2t = {}
  25.   for n=0, r1r do
  26.     table.insert(r1t,tonumber(r1l)+n)
  27.   end
  28.   for n=0, r2r do
  29.     table.insert(r2t,tonumber(r2l)+n)
  30.   end
  31.   if r1r-r2r<=0 then
  32.     for k, v in pairs(r1t) do
  33.       if fullMatch then
  34.         if not table.contains(r2t,v) then fullMatch = false end
  35.       end
  36.     end
  37.   end
  38.   if r2r-r1r<=0 then
  39.     for k, v in pairs(r2t) do
  40.       if fullMatch then
  41.         if not table.contains(r1t,v) then fullMatch = false end
  42.       end
  43.     end
  44.   end
  45.   return fullMatch
  46. end
  47.  
  48. function checkRangesPartial(range1,range2)
  49.   local r1l = ""
  50.   local r1u = ""
  51.   local r2l = ""
  52.   local r2u = ""
  53.   local r1r = 0
  54.   local r2r = 0
  55.   local delimit = false
  56.   local anyMatch = false
  57.   print(range1.." "..range2)
  58.   for c=1, #range1 do
  59.     if range1:sub(c,c) ~= "-" and not delimit then r1l = r1l..range1:sub(c,c)
  60.     elseif range1:sub(c,c) == "-" then delimit = true
  61.     elseif range1:sub(c,c) ~= "-" and delimit then r1u = r1u..range1:sub(c,c) else print("this shouldn't be running") end
  62.   end
  63.   delimit = false
  64.   for c=1, #range2 do
  65.     if range2:sub(c,c) ~= "-" and not delimit then r2l = r2l..range2:sub(c,c)
  66.     elseif range2:sub(c,c) == "-" then delimit = true
  67.     elseif range2:sub(c,c) ~= "-" and delimit then r2u = r2u..range2:sub(c,c) else print("this shouldn't be running")  end
  68.   end
  69.   r1r = tonumber(r1u) - tonumber(r1l)
  70.   r2r = tonumber(r2u) - tonumber(r2l)
  71.   local r1t = {}
  72.   local r2t = {}
  73.   for n=0, r1r do
  74.     table.insert(r1t,tonumber(r1l)+n)
  75.   end
  76.   for n=0, r2r do
  77.     table.insert(r2t,tonumber(r2l)+n)
  78.   end
  79.   for k, v in pairs(r1t) do
  80.     if not anyMatch then
  81.       if table.contains(r2t,v) then anyMatch = true print("a match was found, exiting...   "..v) end
  82.     end
  83.   end
  84.   for k, v in pairs(r2t) do
  85.     if not anyMatch then
  86.       if table.contains(r1t,v) then anyMatch = true print("a match was found, exiting...   "..v) end
  87.     end
  88.   end
  89.   return anyMatch
  90. end
  91.  
  92.  
  93. function table.contains(table, element)
  94.   for _, e in pairs(table) do
  95.     if e == element then
  96.       return true
  97.     end
  98.   end
  99. end
  100.  
  101. local tasks = io.open("input","r")
  102. local fullMatches = 0
  103. local anyMatches = 0
  104. for sets in io.lines("input") do
  105.   local range1 = ""
  106.   local range2 = ""
  107.   local delimit = false
  108.   local temp = tasks:read()
  109.   for c=1, #temp do
  110.     if temp:sub(c,c) ~= "," and not delimit then range1 = range1..temp:sub(c,c)
  111.     elseif temp:sub(c,c) == "," then delimit = true
  112.     elseif temp:sub(c,c) ~= "," and delimit then range2 = range2..temp:sub(c,c) end
  113.   end
  114.   if checkRangesFull(range1,range2) then fullMatches = fullMatches + 1 end
  115.   if checkRangesPartial(range1,range2) then anyMatches = anyMatches + 1 end
  116. end
  117.  
  118. print(fullMatches)
  119. print(anyMatches)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement