Part 1 done
This commit is contained in:
parent
dee95b5164
commit
69f12d9c92
5 changed files with 216 additions and 0 deletions
1
day_2/answer1
Normal file
1
day_2/answer1
Normal file
|
@ -0,0 +1 @@
|
|||
Sum of valid game ids: 2486
|
5
day_2/lua/autorun/solsd_init.lua
Executable file
5
day_2/lua/autorun/solsd_init.lua
Executable file
|
@ -0,0 +1,5 @@
|
|||
if SERVER then
|
||||
util.AddNetworkString("AOC:OpenMenu")
|
||||
util.AddNetworkString("AOC:Part1")
|
||||
util.AddNetworkString("AOC:Part2")
|
||||
end
|
53
day_2/lua/entities/solver/cl_init.lua
Executable file
53
day_2/lua/entities/solver/cl_init.lua
Executable file
|
@ -0,0 +1,53 @@
|
|||
include("shared.lua")
|
||||
|
||||
function ENT:Draw()
|
||||
self:DrawModel()
|
||||
end
|
||||
|
||||
net.Receive("AOC:OpenMenu", function()
|
||||
local ent = net.ReadEntity()
|
||||
|
||||
local win_w = 400
|
||||
local win_h = 300
|
||||
|
||||
local margin = 10
|
||||
local btn_h = 35
|
||||
local btn_w = (win_w - margin * 3) / 2
|
||||
|
||||
local main = vgui.Create("DFrame")
|
||||
main:SetPos(ScrW() / 2, ScrH() / 2)
|
||||
main:SetSize(win_w, win_h)
|
||||
main:SetTitle("Advent of code 2023")
|
||||
main:SetVisible(true)
|
||||
main:SetDraggable(true)
|
||||
main:ShowCloseButton(true)
|
||||
main:MakePopup()
|
||||
|
||||
local input = vgui.Create("DTextEntry", main)
|
||||
input:SetPos(margin, 25 + margin)
|
||||
input:SetSize(win_w - margin * 2, win_h - btn_h - margin * 3 - 25)
|
||||
input:SetEnterAllowed(true)
|
||||
input:SetMultiline(true)
|
||||
|
||||
local part1 = vgui.Create("DButton",main)
|
||||
part1:SetText("Part 1")
|
||||
part1:SetPos(margin, win_h - btn_h - margin)
|
||||
part1:SetSize(btn_w, btn_h)
|
||||
part1.DoClick = function()
|
||||
net.Start("AOC:Part1")
|
||||
net.WriteEntity(self)
|
||||
net.WriteString(input:GetValue())
|
||||
net.SendToServer()
|
||||
end
|
||||
|
||||
local part2 = vgui.Create("DButton",main)
|
||||
part2:SetText("Part 2")
|
||||
part2:SetPos(btn_w + margin * 2, win_h - btn_h - margin)
|
||||
part2:SetSize(btn_w, btn_h)
|
||||
part2.DoClick = function()
|
||||
net.Start("AOC:Part2")
|
||||
net.WriteEntity(self)
|
||||
net.WriteString(input:GetValue())
|
||||
net.SendToServer()
|
||||
end
|
||||
end)
|
146
day_2/lua/entities/solver/init.lua
Executable file
146
day_2/lua/entities/solver/init.lua
Executable file
|
@ -0,0 +1,146 @@
|
|||
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)
|
11
day_2/lua/entities/solver/shared.lua
Executable file
11
day_2/lua/entities/solver/shared.lua
Executable file
|
@ -0,0 +1,11 @@
|
|||
ENT.Type = "anim"
|
||||
ENT.Base = "base_gmodentity"
|
||||
|
||||
ENT.PrintName = "Solver"
|
||||
ENT.Spawnable = true
|
||||
ENT.Category = "Advent of Code 2023"
|
||||
|
||||
|
||||
function ENT:SetupDataTables()
|
||||
self:NetworkVar("String", "", "Input")
|
||||
end
|
Loading…
Reference in a new issue