-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.js
105 lines (82 loc) · 4.56 KB
/
day02.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const input = `RRLUDDLDUDUDUDRDDDRDDRLUUUDRUDURURURLRDDULLLDRRRRULDDRDDURDLURLURRUULRURDDDDLDDRRLDUDUUDURURDLDRRURDLLLDLLRUDRLDDRUULLLLLRRLDUDLUUDRUULLRLLLRLUURDLDLLDDRULDLUURRURLUUURLLDDULRDULULRULDDLRDDUUDLRRURLLURURLDDLURRLUURRRRLDRDLDUDRUDDRULLDUDDLRRLUUUUUDDLLDRLURDDRLLUDULDRDDLLUURUUUURDRLRLLULUULULLRRDLULRUDURDLRLRDDDRULLUULRURULLLUDUURUUUURUULDURDRRRULRLULDLRRULULUUDDDRDURLLURLLDUUUUDULRDLRDUUDDLDUDRLLRLRRRLULUDDDURLRRURUDDDRDRDRLLRDRDLDDRRDRDLLRLLLRRULRDDURRDUDRURDLDULLRRLURLRLLDURRRLLDRRURRRUULDRLDUULRDLDLURUDLLDLLUUDDDUUUDRL
DLRRDRRDDRRDURLUDDDDDULDDLLDRLURDDDDDDRDDDRDDDLLRRULLLRUDULLDURULRRDLURURUDRUURDRLUURRUDRUULUURULULDDLLDDRLDUDDRDRDDUULDULDDLUDUDDUDLULLUDLLLLLRRRUURLUUUULRURULUDDULLLRLRDRUUULULRUUUULRDLLDLDRDRDRDRRUUURULDUUDLDRDRURRUDDRLDULDDRULRRRLRDDUUDRUDLDULDURRDUDDLULULLDULLLRRRDULLLRRURDUURULDRDURRURRRRDLDRRUDDLLLDRDRDRURLUURURRUUURRUDLDDULDRDRRURDLUULDDUUUURLRUULRUURLUUUDLUDRLURUDLDLDLURUURLDURDDDDRURULLULLDRDLLRRLDLRRRDURDULLLDLRLDR
URURLLDRDLULULRDRRDDUUUDDRDUURULLULDRLUDLRUDDDLDRRLURLURUUDRLDUULDRDURRLLUDLDURRRRLURLDDRULRLDULDDRRLURDDRLUDDULUDULRLDULDLDUDRLLDDRRRDULLDLRRLDRLURLUULDDDDURULLDLLLDRRLRRLLRDDRDLDRURRUURLLDDDLRRRRRDLRRDRLDDDLULULRLUURULURUUDRULRLLRDLDULDRLLLDLRRRUDURLUURRUDURLDDDRDRURURRLRRLDDRURULDRUURRLULDLUDUULDLUULUDURRDDRLLLRLRRLUUURRDRUULLLRUUURLLDDRDRULDULURRDRURLRRLRDURRURRDLDUDRURUULULDDUDUULDRDURRRDLURRLRLDUDRDULLURLRRUDLUDRRRULRURDUDDDURLRULRRUDUUDDLLLURLLRLLDRDUURDDLUDLURDRRDLLRLURRUURRLDUUUUDUD
DRRDRRRLDDLDUDRDLRUUDRDUDRRDUDRDURRDDRLLURUUDRLRDDULLUULRUUDDRLDLRULDLRLDUDULUULLLRDLURDRDURURDUDUDDDRRLRRLLRULLLLRDRDLRRDDDLULDLLUUULRDURRULDDUDDDURRDRDRDRULRRRDRUDLLDDDRULRRLUDRDLDLDDDLRLRLRLDULRLLRLRDUUULLRRDLLRDULURRLDUDDULDDRLUDLULLRLDUDLULRDURLRULLRRDRDDLUULUUUULDRLLDRDLUDURRLLDURLLDDLLUULLDURULULDLUUDLRURRRULUDRLDRDURLDUDDULRDRRDDRLRRDDRUDRURULDRRLUURUDULDDDLRRRRDRRRLLURUURLRLULUULLRLRDLRRLLUULLDURDLULURDLRUUDUUURURUURDDRLULUUULRDRDRUUDDDRDRL
RLRUDDUUDDDDRRLRUUDLLDRUUUDRRDLDRLRLLDRLUDDURDLDUDRRUURULLRRLUULLUDRDRUDDULRLLUDLULRLRRUUDLDLRDDDRDDDUDLULDLRRLUDUDDRRRRDRDRUUDDURLRDLLDLDLRRDURULDRLRRURULRDDLLLRULLRUUUDLDUURDUUDDRRRDDRLDDRULRRRDRRLUDDDRUURRDRRDURDRUDRRDLUDDURRLUDUDLLRUURLRLLLDDURUDLDRLRLLDLLULLDRULUURLDDULDDRDDDURULLDRDDLURRDDRRRLDLRLRRLLDLLLRDUDDULRLUDDUULUDLDDDULULDLRDDLDLLLDUUDLRRLRDRRUUUURLDLRRLDULURLDRDURDDRURLDLDULURRRLRUDLDURDLLUDULDDU`;
const inputDemo = `ULL
RRDDD
LURDL
UUUUD`;
const layout1 = [
1, 2, 3,
4, 5, 6,
7, 8, 9
]
const layout2 = [
0,0,1,0,0,
0,2,3,4,0,
5,6,7,8,9,
0,10,11,12,0,
0,0,13,0,0,
]
export class Numpad{
constructor(width, height, startRow = 0, startColumn = 0, keyLayout){
this.keyLayout = keyLayout;
this.keys = [];
this.rows = width;
this.columns = height;
this.currentRow = startRow;
this.currentColumn = startColumn;
}
get value(){
return this.keyLayout[this.index].toString(16);
}
get index(){
return this.currentRow * this.rows + this.currentColumn;
}
move({columns, rows}) {
if(this.canMove(columns, rows)){
this.currentColumn = this.currentColumn + columns;
this.currentRow = this.currentRow + rows;
return this.index;
}
return false;
}
canMove(columns = 0, rows = 0) {
let newIndex = (this.currentRow + rows) * this.columns + (this.currentColumn + columns);
return this.keyLayout[newIndex] > 0 && this.inBounds(columns, rows);
}
inBounds(columns, rows){
return (this.currentRow + rows) < this.rows &&
(this.currentRow + rows) >= 0 &&
(this.currentColumn + columns) < this.columns &&
(this.currentColumn + columns) >= 0;
}
saveKey(){
this.keys.push(this.value);
}
getCombination(){
return this.keys.join('-').toUpperCase();
}
}
const DIRECTIONS = {
'U': {columns: 0, rows: -1, name: 'UP'},
'D': {columns: 0, rows: 1, name: 'DOWN'},
'L': {columns: -1, rows: 0, name: 'LEFT'},
'R': {columns: 1, rows: 0, name: 'RIGHT'}
}
export function run(){
let numpad = new Numpad(3, 3, 1, 1, layout1);
//numpad = new Numpad(5, 5, 2, 0, layout2);
let commandsPerKey = inputDemo.split('\n');
commandsPerKey = input.split('\n');
commandsPerKey.forEach(keyTokens => {
let commands = keyTokens.split('');
commands.forEach(token => {
let direction = DIRECTIONS[token];
numpad.move(direction);
});
numpad.saveKey();
});
console.log(numpad.getCombination());
}