---
id: 6a15cadf5f240d05a264955e
title: "Challenge 320: Blood Bank"
challengeType: 29
dashedName: challenge-320
---

# --description--

Given an array of the inventory at a blood bank and an array of patient blood type requests, return a string in the format `"X of Y patients served"`. Where `X` is the maximum number of patients that can receive blood from the bank's inventory, and `Y` is the total number of patients.

Each entry in both arrays is one of the following blood types: `"AB"`, `"A"`, `"B"`, or `"O"`.

Compatibility rules:

- `"AB"` can receive from any blood type.
- `"A"` can receive from `"A"` and `"O"`.
- `"B"` can receive from `"B"` and `"O"`.
- `"O"` can only receive from `"O"`.

Duplicate entries in the given arrays represent quantity.

# --hints--

`triage_blood(["O", "A", "B", "AB"], ["O", "A", "B", "AB"])` should return `"4 of 4 patients served"`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(triage_blood(["O", "A", "B", "AB"], ["O", "A", "B", "AB"]), "4 of 4 patients served")`)
}})
```

`triage_blood(["A", "A", "B", "B", "AB"], ["O", "A", "B", "B", "B"])` should return `"3 of 5 patients served"`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(triage_blood(["A", "A", "B", "B", "AB"], ["O", "A", "B", "B", "B"]), "3 of 5 patients served")`)
}})
```

`triage_blood(["O", "A", "B", "AB"], ["AB", "AB", "AB", "AB", "AB"])` should return `"4 of 5 patients served"`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(triage_blood(["O", "A", "B", "AB"], ["AB", "AB", "AB", "AB", "AB"]), "4 of 5 patients served")`)
}})
```

`triage_blood(["O", "O", "O", "O", "O"], ["O", "A", "B", "AB"])` should return `"4 of 4 patients served"`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(triage_blood(["O", "O", "O", "O", "O"], ["O", "A", "B", "AB"]), "4 of 4 patients served")`)
}})
```

`triage_blood(["A", "O", "B", "AB", "B", "AB", "O", "A", "A"], ["O", "A", "B", "AB", "A", "B", "A", "A", "B", "A", "B"])` should return `"8 of 11 patients served"`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(triage_blood(["A", "O", "B", "AB", "B", "AB", "O", "A", "A"], ["O", "A", "B", "AB", "A", "B", "A", "A", "B", "A", "B"]), "8 of 11 patients served")`)
}})
```

`triage_blood(["O", "B", "AB", "AB", "O", "A", "A", "AB", "O", "B", "B", "AB", "A", "B", "AB"], ["O", "A", "B", "B", "A", "B", "AB", "A", "B", "A", "O", "AB", "AB", "O"])` should return `"13 of 14 patients served"`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(triage_blood(["O", "B", "AB", "AB", "O", "A", "A", "AB", "O", "B", "B", "AB", "A", "B", "AB"], ["O", "A", "B", "B", "A", "B", "AB", "A", "B", "A", "O", "AB", "AB", "O"]), "13 of 14 patients served")`)
}})
```

# --seed--

## --seed-contents--

```py
def triage_blood(bank, patients):

    return bank
```

# --solutions--

```py
def triage_blood(bank, patients):
    b = {}
    p = {}
    for t in bank:
        b[t] = b.get(t, 0) + 1
    for t in patients:
        p[t] = p.get(t, 0) + 1

    saved = 0

    def serve(patient, donors):
        nonlocal saved
        for donor in donors:
            n = min(p.get(patient, 0), b.get(donor, 0))
            p[patient] = p.get(patient, 0) - n
            b[donor] = b.get(donor, 0) - n
            saved += n

    serve("O", ["O"])
    serve("A", ["A", "O"])
    serve("B", ["B", "O"])
    serve("AB", ["AB", "A", "B", "O"])
    return f"{saved} of {len(patients)} patients served"
```
