Multi-user CRUD: Valid, Problem, or Error?
JavaScript (ES6), 36 bytes
Without a lookup table
(m,a,f,A,F)=>f-F?2:a^A?a*A&8:a&4?m:a
Try it online!
I/O
- Locking mode (\$m\$): \$0\$ = On, \$8\$ = Off
- Actions (\$a\$ and \$A\$): \$0\$ = Create, \$2\$ = Read, \$4\$ = Update, \$8\$ = Delete
- Files (\$f\$ and \$F\$): any integers
- Output: \$0\$ = Error, \$2\$ = Valid, \$8\$ = Problem
How?
If the files are different, all operations are safe and we just return \$2\$ (valid).
If the files are identical, we need to return:
- \$2\$ (valid) if we have two read operations
- \$8\$ (problem) if we have two delete operations, or one update and one read
- \$m\$ (either problem or error) if we have two update operations
- \$0\$ (error) for everything else
Using a \$4\times4\$ CRUD matrix (which is symmetric by definition), we can see that the above values can be computed with:
$$\underbrace{\text{a ^ A}}_{a\neq A?}\text{ ? }\color{blue}{\text{a * A & 8}}\text{ : }\underbrace{\text{a & 4}}_{\text{update?}}\text{ ? }\color{red}{\text{m}} : \color{magenta}{\text{a}}$$
$$\begin{array}{cc|cccc} &&\text{C}&\text{R}&\text{U}&\text{D}\\ &&0&2&4&8\\ \hline \text{C}&0&\color{magenta}{0}&\color{blue}{0}&\color{blue}{0}&\color{blue}{0}\\ \text{R}&2&\color{blue}{0}&\color{magenta}{2}&\color{blue}{8}&\color{blue}{0}\\ \text{U}&4&\color{blue}{0}&\color{blue}{8}&\color{red}{\text{m}}&\color{blue}{0}\\ \text{D}&8&\color{blue}{0}&\color{blue}{0}&\color{blue}{0}&\color{magenta}{8} \end{array}$$
JavaScript (ES6), 46 45 40 bytes
With a lookup table
(m,a,f,A,F)=>f-F?0:[m,1,1,0][a*2+A*9&23]
Try it online!
I/O
- Locking mode: undefined = On, \$1\$ = Off
- Actions: \$0\$ = Update, \$1\$ = Read, \$2\$ = Create, \$3\$ = Delete
- Files: any integers
- Output: undefined = Error, \$0\$ = Valid, \$1\$ = Problem
Retina 0.8.2, 53 bytes
^(.)(?!\1).+|..RR.
V
..DD.
P
..UUL
E
.+[CD].+
E
..+
P
Try it online! Link includes test suite. Takes input as a string of 5 characters, two characters representing the file names, then two characters from CRUD
, then L
or U
(locked/unlocked), and outputs one of VPE
(valid/problem/error). Explanation:
^(.)(?!\1).+|..RR.
V
Different file names are always valid, as are two reads. Annoyingly, this is the only test that forces me to use a header. (It would cost an extra byte to make the header unnecessary.)
..DD.
P
Two deletes are always a problem.
..UUL
E
Two locked updates are an error.
.+[CD].+
E
Any other creates or deletes are an error.
..+
P
Everything else is a problem.
Octave, 96 bytes
@(a,b,c)[a(1)!=b(1)|a(2)+b(2)==20,mod((m=a+b+c)(2),10010)<1|mod(m(2),1020000)<1|mod(m(2),200)<1]
Try it online!
Definitely can be shorter, but I don't have time right now to do that
File 1 = 0
File 2 = 1
Read = 10
Delete = 100
Create = 1000
Update = 10000
Lock on = 100000
Lock off = 1000000
Valid Values:
[1 0]
Problem Values:
[0 1]
Invalid Values:
[0 0]
Input as a = [file, action], b = [file2, action2], c = lock