day 3 with hopeful counting for part 2
This commit is contained in:
parent
3db67bd012
commit
6527698b9c
4 changed files with 1069 additions and 0 deletions
24
day03/part1.lua
Normal file
24
day03/part1.lua
Normal file
|
@ -0,0 +1,24 @@
|
|||
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)
|
33
day03/part2.lua
Normal file
33
day03/part2.lua
Normal file
|
@ -0,0 +1,33 @@
|
|||
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
|
Loading…
Add table
Add a link
Reference in a new issue