r/adventofcode • u/Okasan_ • 21d ago
Help/Question [2021 day 3 (Part 2)][Lua] I'm losing my mind...
I just don't get it, getting the oxygen generator rating works fine and gets the correct answer at least in the example but getting the co2 scrubber rating just returns and empty table, when it should return a table with the binary numbers. here's the main file:
local pf = require("puzzleFuncs")
local function split(inp)
local t = {}
for i,v in ipairs(inp) do
table.insert(t, {})
for c in v:gmatch(".") do
table.insert(t[i], c)
end
end
return t
end
local function getGaEpRates(diag)
local gamma, epsilon = '', ''
local d = {table.unpack(diag)}
local binaryLen = #d[1]
for i = 1, binaryLen do
local ones, zeros = 0, 0
for _,v in ipairs(d) do
if v[i] == "1" then
ones = ones + 1
else
zeros = zeros + 1
end
end
if ones > zeros then
gamma = gamma .. "1"
else
gamma = gamma .. "0"
end
if ones < zeros then
epsilon = epsilon .. "1"
else
epsilon = epsilon .. "0"
end
end
return gamma, epsilon
end
local function conv2Obj(inp)
local t = {}
for i,v in ipairs(inp) do
table.insert(t, { binary = {}, remove = false })
for c in v:gmatch(".") do
table.insert(t[i].binary, c)
end
end
return t
end
local function binaryString(t)
local bin = ''
for i,v in ipairs(t) do
bin = bin .. v
end
return bin
end
local function getRemoved(t)
local count = 0
for i,v in ipairs(t) do
if v.remove then
count = count + 1
end
end
return count
end
local function getBinary(t)
local bin = {}
for i,v in ipairs(t) do
if not v.remove then
bin = v.binary
break
end
end
return bin
end
local function getOxygenRating(diag)
local d = {table.unpack(diag)}
local binaryLen = #d[1].binary
for i = 1, binaryLen do
if getRemoved(d) == 1 then
break
end
local ones, zeros = 0, 0
for j,v in ipairs(d) do
if v.binary[i] == '1' and not v.remove then
ones = ones + 1
elseif v.binary[i] == '0' and not v.remove then
zeros = zeros + 1
end
end
if ones > zeros then
for j,v in ipairs(d) do
if v.binary[i] == '0' and not v.remove then
v.remove = true
end
end
elseif zeros > ones then
for j,v in ipairs(d) do
if v.binary[i] == '1' and not v.remove then
v.remove = true
end
end
else
for j,v in ipairs(d) do
if v.binary[i] == '0' and not v.remove then
v.remove = true
end
end
end
end
return getBinary(d)
end
local function getCO2Rating(diag)
local d = {table.unpack(diag)}
local binaryLen = #d[1].binary
for i = 1, binaryLen do
if getRemoved(d) == 1 then
break
end
local ones, zeros = 0, 0
for j,v in ipairs(d) do
if v.binary[i] == '1' and not v.remove then
ones = ones + 1
elseif v.binary[i] == '0' and not v.remove then
zeros = zeros + 1
end
end
if ones < zeros then
for j,v in ipairs(d) do
if v.binary[i] == '0' and not v.remove then
v.remove = true
end
end
elseif zeros < ones then
for j,v in ipairs(d) do
if v.binary[i] == '1' and not v.remove then
v.remove = true
end
end
else
for j,v in ipairs(d) do
if v.binary[i] == '0' and not v.remove then
v.remove = true
end
end
end
end
return getBinary(d)
end
local input = pf.readFile("example.txt")
local diagnostic = split(input)
local gamma, epsilon = getGaEpRates(diagnostic)
local powerComsuption = tonumber(gamma, 2) * tonumber(epsilon, 2)
local binaryObjs = conv2Obj(input)
local binaryObjs2 = conv2Obj(input)
local oxygenRating = getOxygenRating(binaryObjs)
local CO2Rating = getCO2Rating(binaryObjs2)
print('oxygen rating')
pf.printTable(oxygenRating)
print('co2 rating')
pf.printTable(CO2Rating)I remade it many times the getCO2Rating function if there was something wrong i was missing, also copypasted the getOxygenRating and rename it to getCO2Rating since it does the same thing just with different if statements and for some reason doesnt return nothing, because it doesnt stop the for loop, its not running the if statement checking if there's only one binary left
