Trawler Quota Log
MediumOnline Assessment
Keep the landing log of a fishing fleet whose catch is limited by zone quotas and by how much each boat may bring ashore per trip.
quotas lists [species, zone, kg]: the kilograms of that species still allowed to be landed from that zone. A species-zone pair that is not listed has no quota (0 kg). trip_cap is the most any boat may land during one trip, summed over all species and zones. Process log in order; each entry is one of:
"catch BOAT SPECIES ZONE KG": the boat hauls inKGkilograms. It lands as much as it can: the least ofKG, the remaining quota for that species in that zone, and what is left of the boat'strip_capon its current trip. The landed amount is deducted from the quota and counts toward the boat's trip. The rest is discarded at sea. A boat that has not returned yet is on its first trip, and a boat starts every trip with a fulltrip_cap."return BOAT": the boat comes into port; its next catch starts a new trip."transfer SPECIES FROM TO KG": moveKGkilograms of quota for that species from zoneFROMto zoneTO. It is done only if the *remaining* quota of the species inFROMis at leastKG; otherwise it is ignored entirely. A pair that did not exist is created by a transfer into it.
Input: quotas, trip_cap, log.
Output: [boats, remaining, discarded].
boats:[boat, total landed]for every boat that appears in acatch, sorted by boat name (a boat that landed nothing still appears with 0).remaining:[species, zone, kg left]for every pair that was listed inquotasor created by a transfer (including those at 0), sorted by species and then zone.discarded: the total kilograms discarded.
Example 1
- Input:
- [[["cod","north",100],["cod","south",50],["haddock","north",80]],120,["catch ana cod north 70","catch ana haddock north 60","catch ana cod south 10","return ana","catch ana cod north 40","transfer cod south north 30","catch ben cod north 25","catch ben hake south 15","transfer cod south north 30","transfer haddock north north 5"]]
- Output:
- [[["ana",150],["ben",25]],[["cod","north",5],["cod","south",20],["haddock","north",30]],45]
- Explanation
- Ana's first haul lands 70 kg of cod. The haddock haul of 60 only has 50 kg of trip allowance left (120 - 70), so 10 kg is discarded; the cod from the south then gets nothing (10 discarded). After she returns, only 30 kg of the north cod quota is left, so 30 of her 40 kg lands (10 discarded). The transfer of 30 kg south to north works because the south still has 50. Ben lands 25 kg of north cod; hake has no quota, so his 15 kg is discarded. The second transfer fails (only 20 kg left in the south), and the transfer from a zone to itself changes nothing. Discards total 10 + 10 + 10 + 15 = 45.
Example 2
- Input:
- [[["tuna","east",40],["tuna","west",0]],25,["catch zed tuna east 30","transfer tuna east west 20","catch zed tuna west 10","return zed","catch zed tuna west 10","catch zed tuna east 15","transfer tuna east west 5"]]
- Output:
- [[["zed",40]],[["tuna","east",0],["tuna","west",0]],25]
- Explanation
- Zed's first haul is cut to 25 kg by the trip cap (the east quota drops to 15, 5 discarded). Transferring 20 kg from the east fails because only 15 is left. The west quota is 0, so both west hauls are discarded (10 + 10) even after returning to port. The east haul of 15 lands in full. Zed landed 40 kg and 25 kg were discarded.
Example 3
- Input:
- [[["eel","bay",10]],10,["catch b eel pier 5","transfer eel bay pier 4","catch b eel pier 6","catch c eel bay 3"]]
- Output:
- [[["b",4],["c",3]],[["eel","bay",3],["eel","pier",0]],7]
- Explanation
- The first haul has no quota pair (pier) so all 5 kg is discarded. The transfer of 4 kg from bay to pier creates the pier pair. Boat b then lands 4 of 6 kg (2 discarded) and boat c lands 3 kg of the bay quota, leaving 3. In total 7 kg is discarded.
Constraints
- 0 ≤ quotas.length ≤ 30; 0 ≤ kg ≤ 106; names are short lowercase words
- 1 ≤ trip_cap ≤ 106
- 0 ≤ log.length ≤ 60; 1 ≤ KG ≤ 106 in every catch and transfer