adventofcode-2021/day03/part2.lua

34 lines
696 B
Lua
Raw Normal View History

2021-12-03 08:54:29 +01:00
local ZERO = string.byte("0", 1)
local ONE = string.byte("1", 1)
local total = 0
local counts = {}
for line in io.lines(arg[3]) do
for char = 1, #line do
if line:byte(char) == ONE then
counts[char] = (counts[char] or 0) + 1
end
end
total = total + 1
end
local least, most, lmax, mmax = nil, nil, 0, 0
for line in io.lines(arg[3]) do
local lcur, mcur, lin, min = 0, 0, true, true
for char = 1, #line do
if line:byte(char) == ONE then
if counts[char] >= total / 2 then
if min then mcur = mcur + 1 end
else
min = false
end
else
if counts[char] < total / 2 then
if min then mcur = mcur + 1 end
else
min = false
end
end
end
print(line, mcur)
end