Goal
French car license plates look like CG-123-BJ.They are of the form "ab-cde-fg", where ab and fg range from AA to ZZ, and cde ranges from 001 to 999.
The license plates are generated in alphabetical order:
AA-001-AA, AA-002-AA, AA-003-AA, ..., AA-999-AA,
AA-001-AB, AA-002-AB, AA-003-AB, ..., AA-999-AB,
...,
AA-001-ZZ, AA-002-ZZ, AA-003-ZZ, ..., AA-999-ZZ,
AB-001-AA, AB-002-AA, AB-003-AA, ..., AB-999-AA,
...,
ZY-001-ZZ, ZY-002-ZZ, ZY-003-ZZ, ..., ZY-999-ZZ,
ZZ-001-AA, ZZ-002-AA, ZZ-003-AA, ..., ZZ-999-ZZ.
Given the license plate of a car and the number of cars that were registered after that car, calculate the license plate of the last registered car.
n : number of subsequent registered cars
Solution
The idea behind this problem is to generate a number system based on the license plates. The number system has different symbols, the digit positions have different bases and the order of the position is not increasing, that's the difference to a classical number system, but as for the Project Euler Problem 24 we even had a number system with a factorial base. To convert the license plate symbols from the actual to a decimal representation we introduce the following helper functions:
function parseAlpha(c) {
return (c.charCodeAt(0) - 65) * 26 + c.charCodeAt(1) - 65;
}
function formatAlpha(c) {
return String.fromCharCode(c / 26 + 65 | 0) + String.fromCharCode(c % 26 + 65);
}
function parseNum(n) {
return n - 1;
}
function formatNum(n) {
n = String(n + 1);
for (var i = n.length; i < 3; i++) {
n = '0' + n;
}
return n;
}
The first few steps bring the license plate into the right format. This is done by creating an index array, at which position what information can be found, what parsers and formatters are available and what base every position in our number system has. After this configuration part, the license plate is converted to a number and added to the offset we get as a parameter. From now on it's just the typical base convert routine from base 10 to our custom system:
function solve(p, n) {
var ndx = [0, 2, 1];
var parse = [parseAlpha, parseAlpha, parseNum];
var format = [formatAlpha, formatAlpha, formatNum];
var bases = [26 * 26, 26 * 26, 999];
p = p.split("-");
p = p.map((v, i) => parse[i](p[ndx[i]]));
n+= p[0] * bases[2] * bases[1] + p[1] * bases[2] + p[2];
var ret = [];
for (var i = p.length - 1; i >= 0; i--) {
ret[ndx[i]] = format[i](n % bases[i]);
n = n / bases[i] | 0;
}
return ret.join("-");
}
var x = readline();
var n =+readline();
print(solve(x, n));