Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(sizes){
- let [reqSize, ...givenChunks] = sizes;
- const operations = {
- 'Cut': (c) => c/4,
- 'Lap': (c) => c*0.8,
- 'Grind': (c) => c-20,
- 'Etch': (c) => c-2,
- 'X-ray': (c) => c+1,
- 'Transporting and washing': (c) => Math.floor(c)
- }
- function applyOperation(action, rock){
- let counter = 0;
- let Prevchunk = rock;
- while(rock >= reqSize){
- Prevchunk = rock;
- rock = operations[action](rock);
- counter++;
- }
- rock = Prevchunk;
- counter--;
- counter = counter<0 ? 0 : counter;
- if(counter){
- console.log(`${action} x${counter}`);
- rock = operations['Transporting and washing'](rock);
- console.log('Transporting and washing');
- }
- return rock;
- }
- for(chunk of givenChunks){
- console.log(`Processing chunk ${chunk} microns`)
- chunk = applyOperation('Cut', chunk);
- if(chunk==reqSize){
- console.log(`Finished crystal ${chunk} microns`);
- continue;
- }
- chunk = applyOperation('Lap', chunk);
- if(chunk==reqSize){
- console.log(`Finished crystal ${chunk} microns`);
- continue;
- }
- chunk = applyOperation('Grind', chunk);
- if(chunk==reqSize){
- console.log(`Finished crystal ${chunk} microns`);
- continue;
- }
- let counter = 0;
- while(chunk>reqSize){
- chunk = operations['Etch'](chunk);
- counter++;
- }
- if(counter){
- console.log(`Etch x${counter}`);
- chunk = operations['Transporting and washing'](chunk);
- console.log('Transporting and washing');
- }
- if(chunk+1 == reqSize){
- chunk = operations['X-ray'](chunk);
- console.log(`X-ray x1`);
- }
- console.log(`Finished crystal ${chunk} microns`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement