day 3 with hopeful counting for part 2
This commit is contained in:
parent
3db67bd012
commit
6527698b9c
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
|
1000
inputs/03.long
Normal file
1000
inputs/03.long
Normal file
File diff suppressed because it is too large
Load Diff
12
inputs/03.short
Normal file
12
inputs/03.short
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
00100
|
||||||
|
11110
|
||||||
|
10110
|
||||||
|
10111
|
||||||
|
10101
|
||||||
|
01111
|
||||||
|
00111
|
||||||
|
11100
|
||||||
|
10000
|
||||||
|
11001
|
||||||
|
00010
|
||||||
|
01010
|
Loading…
Reference in New Issue
Block a user