adventofcode-2021/day03/part1.lua
2021-12-03 08:54:29 +01:00

25 lines
478 B
Lua

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 gamma, epsilon = 0, 0
for i, count in ipairs(counts) do
gamma, epsilon = gamma * 2, epsilon * 2
if count > total / 2 then
gamma = gamma + 1
else
epsilon = epsilon + 1
end
end
print(gamma * epsilon)