From 0dd0cd08d1b5e26e070a5b5112006e8ca5a635c1 Mon Sep 17 00:00:00 2001 From: nicole Date: Wed, 1 Dec 2021 18:27:30 +0100 Subject: [PATCH] solved day 1 part 2 --- 01-1/.keep | 0 01-1/main.lua | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) delete mode 100644 01-1/.keep create mode 100755 01-1/main.lua diff --git a/01-1/.keep b/01-1/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/01-1/main.lua b/01-1/main.lua new file mode 100755 index 0000000..bb88961 --- /dev/null +++ b/01-1/main.lua @@ -0,0 +1,63 @@ +#!/usr/bin/env lua + +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 +function lines_from(file) + if not file_exists(file) then return {} end + lines = {} + for line in io.lines(file) do + lines[#lines + 1] = line + end + return lines +end + +function atoi_list(strings) + local ints = {} + for i,s in ipairs(strings) do + ints[#ints + 1] = tonumber(s) + end + return ints +end + +function toSlices(ints) + local slices = {} + for k,i in ipairs(ints) do + if(k < (#ints - 1)) then + slices[k] = ints[k] + ints[k+1] + ints[k+2] + end + end + return slices +end + +------------------------------------------------- +file = "input.txt" + +allLines = lines_from(file) +allInts = atoi_list(allLines) +allSlices = toSlices(allInts) + +for a,b in ipairs(allSlices) do + print(b) +end + +last , counter = 0 , 0; + +last = allSlices[1] + +for a,b in ipairs(allSlices) do + if(a == 1) then + else + if(last < b) then + counter = counter + 1 + end + last = b + end +end + +io.write("The answer is ", counter, ". Congrats, I hope!\n")