Skip to content

Commit 5641bb4

Browse files
committed
✨ Days 23 and 24 completed
1 parent c1dfa30 commit 5641bb4

File tree

6 files changed

+289
-0
lines changed

6 files changed

+289
-0
lines changed

2022/challenge-23/README.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Instructions
2+
3+
We are testing a new CPU for the Santa Claus' sleigh. But we still have to program the software that will run on it.
4+
5+
The CPU has available 8 registers, which are named V00..V07. At the start of the program, all the registers contain 0. The CPU supports the following instructions:
6+
7+
MOV Vxx,Vyy: copies the value from register Vxx to register Vyy; MOV n,Vxx: assign the numeric constant n to register Vxx (overwrite if already has a value); ADD Vxx,Vyy: calculates (Vxx + Ryy) and stores the result in Vxx; DEC Vxx: decrements Vxx value by 1. INC Vxx: increments Vxx value by 1. JMP i: jumps to instruction number i if V00 is different to 0. i is guaranteed to be a valid instruction number and 0 would be the first command.
8+
9+
As the CPU is 8-bit, the number it could represent goes from 0 to 255. If you increment the number 255 causes overflow and results in 0. And if you decrement 0, it results in 255. Keep in mind then that number 280 is the same as 24 (280 - 256 = 24).
10+
11+
After the last instruction has been executed, you should return an array with the result for every register. From V00 to V07. Examples:
12+
13+
```javascript
14+
executeCommands([
15+
'MOV 5,V00', // V00 is 5
16+
'MOV 10,V01', // V01 is 10
17+
'DEC V00', // V00 is now 4
18+
'ADD V00,V01', // V00 = V00 + V01 = 14
19+
]);
20+
21+
// Output: [14, 10, 0, 0, 0, 0, 0]
22+
23+
executeCommands([
24+
'MOV 255,V00', // V00 is 255
25+
'INC V00', // V00 is 256, overflows to 0
26+
'DEC V01', // V01 is -1, overflows to 255
27+
'DEC V01', // V01 is 254
28+
]);
29+
// Output: [0, 254, 0, 0, 0, 0, 0]
30+
31+
executeCommands([
32+
'MOV 10,V00', // V00 is 10
33+
'DEC V00', // decrement V00 by 1 <---┐
34+
'INC V01', // increment V01 by 1 |
35+
'JMP 1', // loop until V00 is 0 ----┘
36+
'INC V06', // increment V06 by 1
37+
]);
38+
39+
// Output: [ 0, 10, 0, 0, 0, 0, 1, 0 ]
40+
```
41+
42+
All the commands provided are already validated and guaranteed to be correct.
43+
44+
Based on SpaceX technical interview from CodeSignal

2022/challenge-23/solution.js

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const solve = (commands) => {
2+
const cpu = {
3+
V00: 0,
4+
V01: 0,
5+
V02: 0,
6+
V03: 0,
7+
V04: 0,
8+
V05: 0,
9+
V06: 0,
10+
V07: 0,
11+
};
12+
13+
const checkOverflow = (result) => {
14+
result > 255 ? (result = result - 256) : result;
15+
result < 0 ? (result = 256 + result) : result;
16+
return result;
17+
};
18+
19+
const compile = (command, line) => {
20+
let from, target, result;
21+
switch (command) {
22+
case 'MOV':
23+
[from, target] = line.slice(4).split(',');
24+
cpu[from]
25+
? (cpu[target] = cpu[from])
26+
: (cpu[target] = Number(from));
27+
break;
28+
case 'ADD':
29+
[target, from] = line.slice(4).split(',');
30+
result = checkOverflow(cpu[from] + cpu[target]);
31+
cpu[target] = cpu[from] + cpu[target];
32+
break;
33+
case 'DEC':
34+
target = line.split(' ')[1];
35+
result = checkOverflow(cpu[target] - 1);
36+
cpu[target] = result;
37+
break;
38+
case 'INC':
39+
target = line.split(' ')[1];
40+
result = checkOverflow(cpu[target] + 1);
41+
cpu[target] = result;
42+
break;
43+
case 'JMP':
44+
break;
45+
}
46+
};
47+
48+
for (let i = 0; i < commands.length; i++) {
49+
const current = commands[i];
50+
const order = current.slice(0, 3);
51+
compile(order, current);
52+
}
53+
54+
return Object.values(cpu);
55+
};
56+
57+
/*
58+
solve([
59+
'MOV 5,V00', // V00 is 5
60+
'MOV 10,V01', // V01 is 10
61+
'DEC V00', // V00 is now 4
62+
'ADD V00,V01', // V00 = V00 + V01 = 14
63+
]);
64+
*/
65+
66+
/*
67+
solve([
68+
'MOV 255,V00', // V00 is 255
69+
'INC V00', // V00 is 256, overflows to 0
70+
'DEC V01', // V01 is -1, overflows to 255
71+
'DEC V01', // V01 is 254
72+
]);
73+
*/
74+
75+
solve([
76+
'MOV 10,V00', // V00 is 10
77+
'DEC V00', // decrement V00 by 1 <---┐
78+
'INC V01', // increment V01 by 1 |
79+
'JMP 1', // loop until V00 is 0 ----┘
80+
'INC V06', // increment V06 by 1
81+
]);
82+
83+
module.exports = { solve };

2022/challenge-23/solution.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const { solve } = require('./solution');
2+
3+
test('Test challenge #', () => {
4+
expect(
5+
solve(['MOV 5,V00', 'MOV 10,V01', 'DEC V00', 'ADD V00,V01']),
6+
).toEqual([14, 10, 0, 0, 0, 0, 0, 0]);
7+
8+
expect(solve(['MOV 255,V00', 'INC V00', 'DEC V01', 'DEC V01'])).toEqual([
9+
0, 254, 0, 0, 0, 0, 0, 0,
10+
]);
11+
});

2022/challenge-24/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Instructions
2+
3+
It's the day! Today we're going to deliver gifts… but the warehouses are a maze and the elves are lost.
4+
5+
Indielfo Jones wants to write a program that, upon receiving a matrix, knows if it can quickly exit the maze from its entrance to the exit.
6+
7+
Mazes have the following positions:
8+
9+
`W`: It's a wall, you can't pass through there. `S`: Entry point to the warehouse. `E`: Exit point from the warehouse. : White spaces are where you can pass through.
10+
11+
Valid movements are from one position up, down, left, or right. You can't move diagonally.
12+
13+
```javascript
14+
canExit([
15+
[' ', ' ', 'W', ' ', 'S'],
16+
[' ', ' ', ' ', ' ', ' '],
17+
[' ', ' ', ' ', 'W', ' '],
18+
['W', 'W', ' ', 'W', 'W'],
19+
[' ', ' ', ' ', ' ', 'E'],
20+
]); // -> true
21+
22+
// You can exit because you start at [0, 4]
23+
// and there's a path to the exit which is [4, 4]
24+
25+
canExit([
26+
[' ', ' ', 'W', 'W', 'S'],
27+
[' ', ' ', ' ', 'W', ' '],
28+
[' ', ' ', ' ', 'W', ' '],
29+
['W', 'W', ' ', 'W', 'W'],
30+
[' ', ' ', ' ', ' ', 'E'],
31+
]); // -> false
32+
```
33+
34+
// There's no way to get from [0, 4] to [4, 4]
35+
36+
Remember that:
37+
38+
- You only have to return whether you can exit the maze with a boolean.
39+
- You can't jump over W walls.
40+
- You can't exit the limits of the matrix, you always have to follow an internal path.

2022/challenge-24/solution.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const solve = (maze) => {
2+
const used = [];
3+
let start = [];
4+
5+
for (let i = 0; i < maze.length; i++) {
6+
for (let j = 0; j < maze[0].length; j++) {
7+
if (maze[i][j] === 'S') {
8+
start = [i, j];
9+
break;
10+
}
11+
}
12+
}
13+
14+
const checkMovement = (coords) => {
15+
return (
16+
maze[coords[0]] &&
17+
maze[coords[0]][coords[1]] &&
18+
maze[coords[0]][coords[1]] !== 'W'
19+
);
20+
};
21+
22+
const scape = (maze, currentPositon) => {
23+
used.push(currentPositon.join(','));
24+
25+
const [currentRow, currentColumn] = currentPositon;
26+
const up = [currentRow, currentColumn + 1];
27+
const down = [currentRow, currentColumn - 1];
28+
const left = [currentRow - 1, currentColumn];
29+
const right = [currentRow + 1, currentColumn];
30+
const possibleMovements = [];
31+
32+
if (checkMovement(up)) possibleMovements.push(up);
33+
if (checkMovement(down)) possibleMovements.push(down);
34+
if (checkMovement(left)) possibleMovements.push(left);
35+
if (checkMovement(right)) possibleMovements.push(right);
36+
37+
return possibleMovements.some(
38+
(movement) =>
39+
maze[movement[0]][movement[1]] === 'E' ||
40+
(!used.includes(movement.join(',')) && scape(maze, movement)),
41+
);
42+
};
43+
44+
const possible = scape(maze, start);
45+
return possible;
46+
};
47+
48+
module.exports = { solve };

2022/challenge-24/solution.spec.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const { solve } = require('./solution');
2+
3+
test('Test challenge #', () => {
4+
expect(
5+
solve([
6+
[' ', ' ', 'W', ' ', 'S'],
7+
[' ', ' ', ' ', ' ', ' '],
8+
[' ', ' ', ' ', 'W', ' '],
9+
['W', 'W', ' ', 'W', 'W'],
10+
[' ', ' ', ' ', ' ', 'E'],
11+
]),
12+
).toBe(true);
13+
14+
expect(
15+
solve([
16+
[' ', ' ', 'W', 'W', 'S'],
17+
[' ', ' ', ' ', 'W', ' '],
18+
[' ', ' ', ' ', 'W', ' '],
19+
['W', 'W', ' ', 'W', 'W'],
20+
[' ', ' ', ' ', ' ', 'E'],
21+
]),
22+
).toBe(false);
23+
24+
expect(
25+
solve([
26+
[' ', ' ', 'W', 'W', 'S'],
27+
[' ', ' ', ' ', 'W', ' '],
28+
[' ', ' ', ' ', 'W', ' '],
29+
['W', 'W', ' ', ' ', 'W'],
30+
[' ', ' ', ' ', ' ', 'E'],
31+
]),
32+
).toBe(false);
33+
34+
expect(solve([['E', ' ', ' ', ' ', 'S']])).toBe(true);
35+
36+
expect(solve([['E', ' ', 'W', ' ', 'S']])).toBe(false);
37+
38+
expect(solve([['E', ' ', 'W', ' ', 'S']])).toBe(false);
39+
40+
expect(
41+
solve([
42+
['E', ' ', 'W', ' ', 'S'],
43+
[' ', ' ', ' ', ' ', ' '],
44+
['W', 'W', 'W', 'W', 'W'],
45+
]),
46+
).toBe(true);
47+
48+
expect(
49+
solve([
50+
['E', ' ', 'W', ' ', 'S'],
51+
[' ', ' ', 'W', ' ', ' '],
52+
['W', 'W', 'W', 'W', 'W'],
53+
]),
54+
).toBe(false);
55+
56+
expect(
57+
solve([
58+
['E', 'S', 'W', 'W', 'W'],
59+
['W', 'W', 'W', 'W', 'W'],
60+
['W', 'W', 'W', 'W', 'W'],
61+
]),
62+
).toBe(true);
63+
});

0 commit comments

Comments
 (0)