Created
December 2, 2020 14:21
-
-
Save JesterXL/2229763e203f0c3a3200b2f3dc24f340 to your computer and use it in GitHub Desktop.
Advent of Code 2020 - Roblox Lua - Day 1
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, reduce = LuaFP.collection.map, LuaFP.collection.reduce | |
function module.main() | |
print("Day 1, Puzzle 1") | |
local input = Puzzle1Input.input() | |
local parsed = string.split(input, '\n') | |
local targetValue = 2020 | |
local numbers = map( | |
tonumber, | |
parsed | |
) | |
local nums = reduce( | |
function(acc, number) | |
local secondNumberAdded = reduce( | |
function(secondAcc, secondNumber) | |
if secondAcc.done == true then | |
return secondAcc | |
end | |
if secondNumber + number == targetValue then | |
return { first = number, second = secondNumber, done = true } | |
end | |
return secondAcc | |
end, | |
{done = false}, | |
numbers | |
) | |
if secondNumberAdded.done == true then | |
return secondNumberAdded | |
end | |
return acc | |
end, | |
{}, | |
numbers | |
) | |
local multipledResult = nums.first * nums.second | |
print("fisrt:", nums.first, "second:", nums.second) | |
print("multipledResult:", multipledResult) | |
workspace.Day1Wall.Day1SurfaceGui.Puzzle1TextLabel.Text = "First: " .. tostring(nums.first) .. "\nSecond: " .. tostring(nums.second) .. "\nResult: " .. tostring(multipledResult) | |
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, reduce = LuaFP.collection.map, LuaFP.collection.reduce | |
function module.main() | |
print("Day 1 Puzzle 2") | |
local input = Puzzle1Input.input() | |
local parsed = string.split(input, '\n') | |
local targetValue = 2020 | |
local numbers = map( | |
tonumber, | |
parsed | |
) | |
local nums = reduce( | |
function(acc, number) | |
return reduce( | |
function(secondAcc, secondNumber) | |
return reduce( | |
function(thirdAcc, thirdNumber) | |
if thirdAcc.done == true then | |
return thirdAcc | |
end | |
if thirdNumber + secondNumber + number == targetValue then | |
return { first = number, second = secondNumber, third = thirdNumber, done = true } | |
end | |
return thirdAcc | |
end, | |
secondAcc, | |
numbers | |
) | |
end, | |
acc, | |
numbers | |
) | |
end, | |
{}, | |
numbers | |
) | |
print("fisrt:", nums.first, "second:", nums.second, "third:", nums.third) | |
local multipledResult = nums.first * nums.second * nums.third | |
print("multipledResult:", multipledResult) | |
workspace.Day1Wall.Day1SurfaceGui.Puzzle2TextLabel.Text = "First: " .. tostring(nums.first) .. "\nSecond: " .. tostring(nums.second) .. "\nThird:" .. tostring(nums.third) .. "\nResult: " .. tostring(multipledResult) | |
end | |
return module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment