Edit: I haven't played D&D before so when I initially made this question I didn't properly research it. I apologize for this, and I'm making a few edits that might invalidate answers to stay as true as possible to the dnd 5e rules. Sorry.
A D&D fan from a recent Hot Network Question seems to have some trouble working out whether a sorcerer's chosen spells line up with the possibilities - and I think we should help!
Introduction
(all of this is already described in the previously mentioned question)
A sorcerer knows two level 1 spells from start (level 1): [1, 1]
Every time a sorcerer gains a level (except for levels 12, 14, 16, 18, 19 and 20) they learn a new spell (mandatory).
Additionally, when leveling up one can choose (optional) to replace one of the spells with another.
The spells learned and replaced must be a valid spell slot level which is half your sorcerer's level rounded up. See this table:
Sorcerer level Highest spell level possible
1 1
2 1
3 2
4 2
5 3
6 3
7 4
8 4
9 5
10 5
11 6
12 6
13 7
14 7
15 8
16 8
17 9
18 9
19 9
20 9
This means at level 3 one can have the spell levels [1, 1, 2, 2]
like this:
Level 1: [1, 1] (initial)
Level 2: [1, 1, 1 (new)]
Level 3: [1, 1, 2 (replaced), 2 (new)]
It is not required to pick the highest level spells you have access to.
The spell levels [1, 1, 1, 1]
are perfectly valid for a level 3.
Lastly, remember that replacing a spell is an optional option for every level. This means that some levels could skip the replace, while others make use of it.
The challenge
Make a program or function that takes an integer (level) between 1 and 20.
It should also take an array of integers (spell levels) with values ranging from 1 to 9 in any order (9 is the maximum spell level).
The output of the program should be a truthy/falsy value validating if the chosen spell levels are valid for a sorcerer of the given level.
Test cases
Level: 1
Spells: [1, 1]
Output: true
Level: 8
Spells: [1, 1, 2, 3, 3, 5]
Ouput: false
Reason: A level 8 can't ever have access to a level 5 spell.
Level: 5
Spells: [1, 1, 1, 2, 2, 2, 3]
Output: false
Reason: A level 5 can't have access to 7 spells
Level: 11
Spells: [3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6]
Output: false
Reason: Too many spell upgrades.
The highest valid selection for level 11 is
[3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6]
This is code-golf - fewest bytes wins!
[2,2,3,3]
would not be possible as it requires more replacing than a sorcerer of that level would have access to." - isn't the fact that the list is length 4 rather than 5 a more fundamental reason here? (I assume[1,3,2,2,3]
is possible for a level 4 by going from the level 3[1,1,2(replaced),2(new)]
to[1,3(replaced),2,2,3(new)]
?) \$\endgroup\$