init & day 1

This commit is contained in:
ame 2023-12-04 02:11:50 -06:00
commit d6e7bcd9ed
5 changed files with 1097 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
src/llib.so

7
license.md Normal file
View File

@ -0,0 +1,7 @@
Copyright © 2023 <ameliasquires@disroot.org>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1001
src/1.input Normal file

File diff suppressed because it is too large Load Diff

24
src/1p1.lua Normal file
View File

@ -0,0 +1,24 @@
require "llib"
local inp = llib.io.readfile("1.input")
local lines = {}
for s in inp:gmatch("[^\r\n]+") do
table.insert(lines, s)
end
local total = 0
for _,s in pairs(lines) do
local firstn = -1;
local lastn = 0;
for c in s:gmatch"." do
if not (tonumber(c) == nil) then
if firstn == -1 then
firstn = tonumber(c);
end
lastn = tonumber(c);
end
end
total = total + (firstn * 10) + lastn;
end
print(total)

64
src/1p2.lua Normal file
View File

@ -0,0 +1,64 @@
require "llib"
local inp = llib.io.readfile("1.input")
local function find_pairs(s)
local list = {"one","two","three","four","five","six","seven","eight","nine"};
local low = {v=nil,ind=string.len(s)+1};
local high = {v=nil,ind=-1};
for k,i in pairs(list) do
local ind = string.find(s, i, 1, true);
while not (ind == nil) do
if ind < low.ind then
low.ind = ind;
low.v = k;
end
if ind > high.ind then
high.ind = ind;
high.v = k;
end
ind = string.find(s, i, ind+1, true);
end
end
return {low = low, high = high};
end
local lines = {}
for s in inp:gmatch("[^\r\n]+") do
table.insert(lines, s)
end
local total = 0
for _,s in pairs(lines) do
local firstn_sel = false;
local firstn = 0;
local lastn = 0;
local m = find_pairs(s);
if not (m.low.v == nil) then
firstn = m.low.v
end
if not (m.high.v == nil) then
lastn = m.high.v
end
local ind = 1;
for c in s:gmatch"." do
if not (tonumber(c) == nil) then
if ind <= m.low.ind and firstn_sel == false then
firstn = tonumber(c);
firstn_sel = true;
end
if ind >= m.high.ind then
lastn = tonumber(c);
end
end
ind=ind+1;
end
total = total + (firstn * 10) + lastn;
end
print(total)