Port C Room State
Lesson 48 of 48 in Coddy's Lua for C Developers course.
Challenge
MediumThe C version stores state in a struct and passes its address to a function. Lua stores that state in a fresh table and passes the receiver with a colon call. No C class or inheritance feature is involved.
Port the C reference. First input line is nonnegative room capacity; remaining lines are signed integer reservation requests. Accept only strictly positive requests that fit. Failed requests leave used seats unchanged. Return remaining:accepted. Implement Model.new(capacity) and Model:reserve(seats) in model.lua; reserve returns a boolean. Use a fresh object per run.
C reference
#include <stdio.h>
#include <stdbool.h>
struct Room { long capacity; long used; };
static bool reserve(struct Room *room, long seats) {
if (seats <= 0 || seats > room->capacity - room->used) return false;
room->used += seats;
return true;
}
int main(void) {
struct Room room = {0, 0};
if (scanf("%ld", &room.capacity) != 1 || room.capacity < 0) return 1;
size_t accepted = 0;
long seats;
int scanned;
while ((scanned = scanf("%ld", &seats)) == 1) {
if (reserve(&room, seats)) ++accepted;
}
if (scanned != EOF || ferror(stdin)) return 1;
printf("%ld:%zu\n", room.capacity - room.used, accepted);
return ferror(stdout) ? 1 : 0;
}
Edit app.lua, model.lua. The visible locked main.lua reads stdin into a sequence of lines, calls app.run(lines), and prints its returned value with one final newline. Do not print diagnostics or change the module names. Empty input gives an empty sequence. All numeric inputs are small enough to avoid overflow.
Public API checks: Model.new(capacity) exposes capacity and used, initially zero. reserve returns a boolean and updates used only for a positive request that fits. Rooms are independent. The locked driver checks this behavior before calling app.run. These checks print nothing when successful.
Try it yourself
-- Public API checks
do
local Model=require("model")
local a,b=Model.new(5),Model.new(2)
assert(a:reserve(6)==false and a.used==0,"oversized reservation must not mutate")
assert(a:reserve(0)==false and a:reserve(-1)==false and a.used==0,"nonpositive requests are rejected")
assert(a:reserve(2)==true and a.used==2,"accepted seats update used")
assert(a:reserve(3)==true and a.used==5 and a.capacity==5,"exact fit succeeds without changing capacity")
assert(b.used==0 and b.capacity==2,"rooms must be independent")
end
local app = require("app")
local lines = {}
for line in io.lines() do lines[#lines + 1] = line end
print(app.run(lines))
All lessons in Lua for C Developers
1First Lua Programs
Run Lua ModulesPrint and CommentNames and Local ScopeNumbers and ConversionStrings and Byte PositionsRecap: Format a Receipt4Functions and Closures
Functions and ArgumentsDefaults and Options TablesMultiple Results and VarargsCallbacks and Captured StateRecap: Pricing Functions2Values and Decisions
Absence and Table FieldsTruth and Default ValuesBranches and BoundsValues and Table IdentityRecap: Admission RulesPractice on your own: Online Lua compiler