-
Notifications
You must be signed in to change notification settings - Fork 36
/
Next-car-license-plate.js
59 lines (53 loc) · 1.51 KB
/
Next-car-license-plate.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
// https://www.codingame.com/ide/puzzle/next-car-license-plate
const zeroFill = (number, width) => {
width -= number.toString().length;
return width > 0 ? Array(width + (/\./.test(number) ? 2 : 1)).join('0') + number : number;
};
const ascii = (a) => a.charCodeAt(0);
const toIndex = (c) => (ascii(c) - ascii('A')) % 26;
const toChar = (c) => String.fromCharCode(c + ascii('A'));
let x = readline().split('-').map((p) => p.split(''));
let n = +readline();
let [hours, mins, secs] = [x[0].map(toIndex), x[2].map(toIndex), +x[1].join('')];
const [HOURS,MIN,SEC] = ['HOURS','MIN','SECONDS'];
let state = SEC;
// We turned the plates to timer with easy states.
while (n > 0) {
if (state === SEC) {
if (secs + n <= 999) {
secs += n;
break;
}
n -= (999 - secs);
secs = 1;
state = MIN;
continue;
}
if (state === MIN) {
mins[1]++;
if (mins[1] === 26) {
mins[1] = 0;
mins[0]++;
if (mins[0] === 26) {
mins[0] = 0;
state = HOURS;
continue;
}
}
n--;
state = SEC;
}
if (state === HOURS) {
hours[1]++;
if (hours[1] === 26) {
hours[1] = 0;
hours[0]++;
if (hours[0] === 26) {
hours[0] = 0;
}
}
n--;
state = SEC;
}
}
console.log([hours.map(toChar).join(''), zeroFill(secs,3), mins.map(toChar).join('')].join('-'));