169 lines
3.7 KiB
Lua
Executable file
169 lines
3.7 KiB
Lua
Executable file
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()
|
|
|
|
local power_sum = 0
|
|
for i, line in pairs(split(input, '\n')) do
|
|
local game = parseGame(line)
|
|
print('Game '..game.id)
|
|
|
|
local min_red_needed = 0
|
|
local min_grn_needed = 0
|
|
local min_blu_needed = 0
|
|
|
|
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 > min_red_needed then min_red_needed = play.red end
|
|
if play.green > min_grn_needed then min_grn_needed = play.green end
|
|
if play.blue > min_blu_needed then min_blu_needed = play.blue end
|
|
end
|
|
local power = min_red_needed * min_grn_needed * min_blu_needed
|
|
print(' power '..power)
|
|
power_sum = power_sum + power
|
|
end
|
|
|
|
print('Sum of powers: '..power_sum)
|
|
end)
|