10
2
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!
1Can we take the spell list sorted how we want it? – Veskah – 2018-09-19T20:25:05.773
What is the maximum spell level for each class level? – Nitrodon – 2018-09-19T21:19:40.167
@Nitrodon I presume 19? – Don Thousand – 2018-09-19T21:21:18.920
@Nitrodon, presumably it's 9 given that the array input can only contain "values ranging from 1 to 9" but the maximum spell level we need to handle should be stated more explicitly in the spec. And it could do with a couple more test cases. Nice challenge, otherwise. – Shaggy – 2018-09-19T22:28:02.257
4>
[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)]
?)>
9
, such that upon becoming leveln
there is an option to upgrade any spell to any level up tomin(9,n-1)
and upon becoming leveln
, excluding those listed, a spell is (always?*) acquired up tomin(9,n-1)
? 2. I would think[1,2,2,3,3]
would be possible at level 4 (see the parenthesised part at the end of my previous comment). * That is - 3. are acquisitions of spells at the levels not listed mandatory or optional?Is it possible to have more test cases? The test cases seem a bit light in regards to the complexity of the question. – Olivier Grégoire – 2018-09-20T09:23:29.610
I'm looking at the table from the linked answer where it says that the maximum for level 4 is [2, 2, 2, 2, 1]. I'm confused as to why it can't be [3, 3, 2, 2, 1]? What you would do is take [2, 2, 1, 1], adding a spell of level 3 and upgrading the last spell to level 3. – Cameron Aavik – 2018-09-20T11:02:07.973
@CameronAavik At level 4, you don't have access to level 3 spells. So you gain a new spell, but it may only be of level 1 or 2. – Olivier Grégoire – 2018-09-20T11:36:36.003
The question states that "they learn a new spell that must be below their current level". If they are level 4 how come they can't access a level 3 spell? – Cameron Aavik – 2018-09-20T11:38:17.340
That's probably a bad wording, if the OP wants to follow D&D rules. If OP wants to follow D&D, then the wording must be changed. If the OP wants to make their own rule, then my answer is invalid. – Olivier Grégoire – 2018-09-20T11:44:09.523
This challenge currently ignores the levels at which sorcerers actually get the higher-level spell slots. A level 10 sorcerer in DnD cannot cast a level 9 spell. Is this intentional? – Deacon – 2018-09-20T12:21:21.543
It would be good to have some test cases for the higher levels, when the spells are not given every level. – archangel.mjj – 2018-09-21T13:02:25.437