Created
December 2, 2020 14:22
-
-
Save JesterXL/353ad0aa00a1117bf04f1c924bcd40b1 to your computer and use it in GitHub Desktop.
Advent of Code 2020 - Roblox Lua - Day 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local module = {} | |
local Puzzle1Input = require(script.Parent.Puzzle1Input) | |
local LuaFP = require(script.Parent.Parent.LuaFP) | |
local map, filter, some, reduce = LuaFP.collection.map, LuaFP.collection.filter, LuaFP.collection.some, LuaFP.collection.reduce | |
local repr = require(workspace.repr) | |
local function containsCount(targetLetter, list) | |
return reduce( | |
function(acc, letter) | |
if letter == targetLetter then | |
return acc + 1 | |
end | |
return acc | |
end, | |
0, | |
list | |
) | |
end | |
local function inRange(min, max, value) | |
return value >= min and value <= max | |
end | |
function module.main() | |
print("Day 2, Puzzle 1") | |
local input = Puzzle1Input.input() | |
local parsed = string.split(input, '\n') | |
local passwords = map( | |
function(passwordString) | |
local passwordSplit = string.split(passwordString, ' ') | |
local passwordRange, passwordLetter, passwordText = table.unpack(passwordSplit) | |
local passwordTextList = string.split(passwordText, "") | |
local rangeMin, rangeMax = table.unpack(string.split(passwordRange, '-')) | |
return { | |
range = passwordRange, | |
rangeMin = tonumber(rangeMin), | |
rangeMax = tonumber(rangeMax), | |
letter = string.sub(passwordLetter, 1, 1), | |
text = passwordText, | |
list = passwordTextList | |
} | |
end, | |
parsed | |
) | |
--for key,value in pairs(passwords[1]) do print("key:", key, "value:", value, "value type:", type(value)) end | |
local legitPasswords = filter( | |
function(password) | |
local letterCount = containsCount(password.letter, password.list) | |
return inRange(password.rangeMin, password.rangeMax, letterCount) | |
end, | |
passwords | |
) | |
print("valid passwords: ", #legitPasswords) | |
local result = [[Total Passwords: %s | |
Legit Passwords: %s]] | |
local resultString = string.format(result, #passwords, #legitPasswords) | |
workspace.Day2Wall.Day2SurfaceGui.Puzzle1TextLabel.Text = resultString | |
end | |
return module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local module = {} | |
local Puzzle1Input = require(script.Parent.Puzzle1Input) | |
local LuaFP = require(script.Parent.Parent.LuaFP) | |
local map, filter, some, reduce = LuaFP.collection.map, LuaFP.collection.filter, LuaFP.collection.some, LuaFP.collection.reduce | |
local repr = require(workspace.repr) | |
--local nthMaybe = LuaFP.array.nthMaybe | |
local function verifyOnlyOncePosition(positionFirst, posisitionSecond, tergetLetter, list) | |
local count = 0 | |
if list[positionFirst] == tergetLetter then | |
count = count + 1 | |
end | |
if list[posisitionSecond] == tergetLetter then | |
count = count + 1 | |
end | |
return count == 1 | |
end | |
local function inRange(min, max, value) | |
return value >= min and value <= max | |
end | |
function module.main() | |
print("Day 2, Puzzle 2") | |
local input = Puzzle1Input.input() | |
local parsed = string.split(input, '\n') | |
local passwords = map( | |
function(passwordString) | |
local passwordSplit = string.split(passwordString, ' ') | |
local passwordRange, passwordLetter, passwordText = table.unpack(passwordSplit) | |
local passwordTextList = string.split(passwordText, "") | |
local positionFirst, positionSecond = table.unpack(string.split(passwordRange, '-')) | |
return { | |
range = passwordRange, | |
positionFirst = tonumber(positionFirst), | |
positionSecond = tonumber(positionSecond), | |
letter = string.sub(passwordLetter, 1, 1), | |
text = passwordText, | |
list = passwordTextList | |
} | |
end, | |
parsed | |
) | |
--for key,value in pairs(passwords[1]) do print("key:", key, "value:", value, "value type:", type(value)) end | |
local legitPasswords = filter( | |
function(password) | |
return verifyOnlyOncePosition(password.positionFirst, password.positionSecond, password.letter, password.list) | |
end, | |
passwords | |
) | |
print("valid passwords: ", #legitPasswords) | |
local result = [[Total Passwords: %s | |
Legit Passwords: %s]] | |
local resultString = string.format(result, #passwords, #legitPasswords) | |
workspace.Day2Wall.Day2SurfaceGui.Puzzle2TextLabel.Text = resultString | |
end | |
return module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment