Grade: 24/30 — Politecnico di Milano, A.Y. 2023/2024
A discrete-time event-driven simulation of an industrial pastry shop, implemented entirely in C with custom data structures (no stdlib containers).
The system manages recipes, a warehouse with expiring ingredient batches, a customer order queue, and a periodic courier — all advancing on a global integer clock where each command consumes one time unit.
| Problem | Solution |
|---|---|
| O(1) avg. lookup for recipes & ingredients | Custom hash map with CityHash + separate chaining, dynamic resize at 95% load |
| Ingredient batches consumed by earliest expiry | Sorted linked list per ingredient, insertion-sorted by expiration date |
| Orders ready for courier sorted by weight then arrival | Sorted insertion into a priority queue with a custom comparator |
| Orders waiting for ingredients | FIFO linked-list queue, re-evaluated on every restock event |
| Courier capacity constraint | Greedy scan of ready orders from heaviest to lightest until capacity is hit |
main.c
├── CityHash — string hashing for O(1) avg. lookups
├── IngredientsHashMap — hash map of ingredients → sorted batch list
├── RecipesHashMap — hash map of recipe name → ingredient requirements
├── OrderQueue — waiting orders (FIFO) + ready orders (sorted by weight/date)
└── Simulation loop — parses stdin commands, advances clock, triggers courier
aggiungi_ricetta <name> <ingredient> <qty> ... → aggiunta | ignorato
rimuovi_ricetta <name> → rimossa | ordini in sospeso | non presente
rifornimento <ingredient> <qty> <expiry> ... → rifornito
ordine <recipe> <quantity> → accettato | rifiutato
Courier fires every n time units, loads orders greedily by descending weight up to capacity c.
# Compile (mirrors the grader's flags)
gcc -Wall -Werror -std=gnu11 -O2 -lm main.c -o main
# Memory safety check (ASAN)
gcc -Wall -Werror -std=gnu11 -O2 -lm -fsanitize=address -g3 main.c -o main
# Run with input file
./main < input.txt > output.txt
# Memory leak / use-after-free analysis (Valgrind Memcheck)
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./main < input.txt
# CPU profiling (Callgrind)
valgrind --tool=callgrind ./main < input.txt && kcachegrind callgrind.out.*
# Heap memory profiling (Massif)
valgrind --tool=massif ./main < input.txt && massif-visualizer massif.out.*| File | Description |
|---|---|
main.c |
Full C source (~1200 lines) |
specifica.pdf |
Project specification |