day1
This commit is contained in:
commit
180a5ea19c
4 changed files with 1144 additions and 0 deletions
1000
day1/input
Normal file
1000
day1/input
Normal file
File diff suppressed because it is too large
Load diff
51
day1/part1.lua
Normal file
51
day1/part1.lua
Normal file
|
@ -0,0 +1,51 @@
|
|||
-- see if the file exists
|
||||
local function file_exists(file)
|
||||
local f = io.open(file, "rb")
|
||||
if f then
|
||||
f:close()
|
||||
end
|
||||
return f ~= nil
|
||||
end
|
||||
|
||||
-- get all lines from a file, returns an empty
|
||||
-- list/table if the file does not exist
|
||||
local function lines_from(file)
|
||||
if not file_exists(file) then
|
||||
return {}
|
||||
end
|
||||
local lines = {}
|
||||
for line in io.lines(file) do
|
||||
lines[#lines + 1] = line
|
||||
end
|
||||
return lines
|
||||
end
|
||||
|
||||
-- tests the functions above
|
||||
local file = "input"
|
||||
local lines = lines_from(file)
|
||||
|
||||
local total = 0
|
||||
|
||||
local left_list = {}
|
||||
local right_list = {}
|
||||
|
||||
-- https://www.lua.org/manual/5.4/manual.html#pdf-ipairs
|
||||
-- print all line numbers and their contents
|
||||
for _, v in ipairs(lines) do
|
||||
for n1, n2 in string.gmatch(v, "(%d+)%s+(%d+)") do
|
||||
-- [](https://stackoverflow.com/a/27434198)
|
||||
-- [Lua Length Operator](https://www.lua.org/manual/5.2/manual.html#3.4.6)
|
||||
left_list[#left_list + 1] = n1
|
||||
right_list[#right_list + 1] = n2
|
||||
end
|
||||
end
|
||||
|
||||
-- https://www.lua.org/manual/5.2/manual.html#pdf-table.sort
|
||||
table.sort(left_list)
|
||||
table.sort(right_list)
|
||||
|
||||
for i, v in ipairs(left_list) do
|
||||
total = total + math.abs(v - right_list[i])
|
||||
end
|
||||
|
||||
io.output():write(total)
|
63
day1/part2.lua
Normal file
63
day1/part2.lua
Normal file
|
@ -0,0 +1,63 @@
|
|||
-- see if the file exists
|
||||
local function file_exists(file)
|
||||
local f = io.open(file, "rb")
|
||||
if f then
|
||||
f:close()
|
||||
end
|
||||
return f ~= nil
|
||||
end
|
||||
|
||||
-- get all lines from a file, returns an empty
|
||||
-- list/table if the file does not exist
|
||||
local function lines_from(file)
|
||||
if not file_exists(file) then
|
||||
return {}
|
||||
end
|
||||
local lines = {}
|
||||
for line in io.lines(file) do
|
||||
lines[#lines + 1] = line
|
||||
end
|
||||
return lines
|
||||
end
|
||||
|
||||
-- tests the functions above
|
||||
local file = "input"
|
||||
local lines = lines_from(file)
|
||||
|
||||
local left_list = {}
|
||||
local right_list = {}
|
||||
|
||||
-- https://www.lua.org/manual/5.4/manual.html#pdf-ipairs
|
||||
-- print all line numbers and their contents
|
||||
for _, v in ipairs(lines) do
|
||||
for n1, n2 in string.gmatch(v, "(%d+)%s+(%d+)") do
|
||||
-- [](https://stackoverflow.com/a/27434198)
|
||||
-- [Lua Length Operator](https://www.lua.org/manual/5.2/manual.html#3.4.6)
|
||||
left_list[#left_list + 1] = n1
|
||||
right_list[#right_list + 1] = n2
|
||||
end
|
||||
end
|
||||
|
||||
local right_list_count_map = {}
|
||||
|
||||
for _, v in ipairs(right_list) do
|
||||
if right_list_count_map[v] ~= nil then
|
||||
right_list_count_map[v] = right_list_count_map[v] + 1
|
||||
else
|
||||
right_list_count_map[v] = 1
|
||||
end
|
||||
end
|
||||
|
||||
local total = 0
|
||||
|
||||
for _, v in ipairs(left_list) do
|
||||
local count
|
||||
if right_list_count_map[v] ~= nil then
|
||||
count = right_list_count_map[v]
|
||||
else
|
||||
count = 0
|
||||
end
|
||||
total = total + (v * count)
|
||||
end
|
||||
|
||||
io.output():write(total)
|
30
template.lua
Normal file
30
template.lua
Normal file
|
@ -0,0 +1,30 @@
|
|||
-- see if the file exists
|
||||
local function file_exists(file)
|
||||
local f = io.open(file, "rb")
|
||||
if f then
|
||||
f:close()
|
||||
end
|
||||
return f ~= nil
|
||||
end
|
||||
|
||||
-- get all lines from a file, returns an empty
|
||||
-- list/table if the file does not exist
|
||||
local function lines_from(file)
|
||||
if not file_exists(file) then
|
||||
return {}
|
||||
end
|
||||
local lines = {}
|
||||
for line in io.lines(file) do
|
||||
lines[#lines + 1] = line
|
||||
end
|
||||
return lines
|
||||
end
|
||||
|
||||
-- tests the functions above
|
||||
local file = "test.lua"
|
||||
local lines = lines_from(file)
|
||||
|
||||
-- print all line numbers and their contents
|
||||
for k, v in pairs(lines) do
|
||||
print("line[" .. k .. "]", v)
|
||||
end
|
Loading…
Reference in a new issue