The ledger is split into files by responsibility, so that each rule lives in one place and can be checked on its own:
main.goconnectsrunto stdin, stdout and a background context, and turns a returned error into a message and exit status 1.types.goholds the limits, the sentinel errors,LineandShortageError.parse.goturns one line of text into aCommand. It validates names and quantities, joins every problem in an order, and sorts order lines.ledger.goowns the state.Stock,OrderandCanceleach apply their whole change or none, andItemsandSnapshotread it.pool.goholds the genericmapConcurrent.app.goreads the bounded input, runs the pipeline and writes the output.
The pipeline in run
run has three stages, and each has its own kind of failure:
- Read.
readCommandsranges overnumberedLines, aniter.Seq2over a scanner whose buffer enforces the 200-byte limit. It skips blank lines and counts commands. A limit failure ends the run before anything else happens. - Parse.
mapConcurrentrunsparseCommandon four workers. A parse error does not stop the pool: it is stored in that command'sparsedvalue. Only cancellation of the context ends this stage early. - Apply. One loop applies each command to a fresh
Ledgerin input order, throughexecute, and writes one line per command to abufio.Writer. A deferredFlushreports a late write failure through the named resulterr.
Open each file in the editor below as you read, and find the function each stage calls.
Why parsing is concurrent and applying is not
Parsing a line needs nothing but the line, so the four workers can parse in any order without affecting one another, and the results are stored by index. Applying a command depends on every command before it: whether order A2 tent:4 fits depends on what A1 reserved. So the ledger applies commands strictly in order, on one goroutine, and never needs a lock. The design keeps all shared state out of the concurrent part.
Trace a rejected order
Follow order A2 tent:4 on line 6 of the sample. Which function decides it is valid input? Which one rejects it, with what error value, and how does that value become the printed line? Why does the rejection leave the drill stock untouched, even in an order that also asks for a drill?
Hint 1: the idea
Valid input and an acceptable request are different questions, answered in different files.
Hint 2: a first step
Start in parseOrder in parse.go, then find where ShortageError is created in ledger.go, then look at the loop at the end of run.
Worked solution
parseOrder accepts the line: A2 is a valid ID, tent a valid item and 4 a valid quantity. The ledger's Order then stages the reservation on maps.Clone(l.stock), finds 3 tents where 4 are needed, and returns fmt.Errorf("order %s: %w", id, &ShortageError{...}). execute passes that error back to run, which formats it as Line 6: followed by the message, replacing any newlines from joined errors with "; ".
An order that also listed a drill would have staged the drill on the copy before failing on the tent. The copy is dropped when Order returns the error, and l.stock is only replaced after every line has passed. So no other line of a rejected order is ever reserved.
Completion is saved in this browser only. It does not measure mastery.