thomas_2023/day_2/lua/entities/solver/init.lua

147 lines
2.9 KiB
Lua
Raw Normal View History

2023-12-03 13:10:13 -05:00
AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")
include("shared.lua")
function ENT:Initialize()
self:SetModel("models/bull/dynamicbutton.mdl")
self:PhysicsInit(SOLID_VPHYSICS)
self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
self:SetUseType(SIMPLE_USE)
local phys = self:GetPhysicsObject()
if IsValid(phys) then
phys:Wake()
phys:SetMass(1)
end
end
function ENT:OnTakeDamage(damage)
self:SetHealth(self:Health() - damage:GetDamage())
if self:Health() <= 0 then
self:Remove()
end
end
function ENT:Think()
self:NextThink( CurTime() + 5 )
return true
end
function ENT:OnRemove()
end
function ENT:Use(activator,caller)
if IsValid(caller) and caller:IsPlayer() then
net.Start("AOC:OpenMenu", false)
net.WriteEntity(self)
net.Send(caller)
else return end
end
function split (inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
local Game = {
id = 0,
plays = {}
}
Game.__index = Game
function Game:new(id)
local game = {}
setmetatable(game, Game)
game.id = id
game.draws = {}
return game
end
local Draw = {
red = 0,
green = 0,
blue = 0,
}
Draw.__index = Draw
function Draw:new()
local draw = {}
setmetatable(draw, Draw)
return draw
end
function parseGame(line)
local id = line:match('%d+')
local game_obj = Game:new(id)
local _, result_idx = line:find('Game %d+: ')
local results = line:sub(result_idx + 1)
local draws = split(results, ';')
for _, draw in pairs(draws) do
local draw_obj = Draw:new()
local cubes = split(draw, ',')
for _, cube in pairs(cubes) do
local n, color = cube:match('(%d+) (%a+)')
draw_obj[color] = tonumber(n)
end
table.insert(game_obj.draws, draw_obj)
end
return game_obj
end
net.Receive("AOC:Part1", function()
local ent = net.ReadEntity()
local input = net.ReadString()
local max_red = 12
local max_grn = 13
local max_blu = 14
local id_sum = 0
for i, line in pairs(split(input, '\n')) do
local game = parseGame(line)
print('Game '..game.id)
local valid = true
for i, play in pairs(game.draws) do
print(' play '..i)
print(' red: '..play.red)
print(' grn: '..play.green)
print(' blu: '..play.blue)
if play.red > max_red or play.green > max_grn or play.blue > max_blu then
valid = false
end
end
if valid then
print('--Valid--')
id_sum = id_sum + game.id
end
end
print('Sum of valid game ids: '..id_sum)
end)
net.Receive("AOC:Part2", function()
local ent = net.ReadEntity()
local input = net.ReadString()
print('Part 2')
print(input)
end)