Advertisement
Martin_D24

Untitled

Mar 10th, 2025
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(sizes){
  2.     let [reqSize, ...givenChunks] = sizes;
  3.  
  4.     const operations = {
  5.         'Cut': (c) => c/4,
  6.         'Lap': (c) => c*0.8,
  7.         'Grind': (c) => c-20,
  8.         'Etch': (c) => c-2,
  9.         'X-ray': (c) => c+1,
  10.         'Transporting and washing': (c) => Math.floor(c)
  11.     }
  12.  
  13.     function applyOperation(action, rock){
  14.         let counter = 0;
  15.         let Prevchunk = rock;
  16.         while(rock >= reqSize){
  17.             Prevchunk = rock;
  18.             rock = operations[action](rock);
  19.  
  20.             counter++;
  21.         }
  22.         rock = Prevchunk;
  23.         counter--;
  24.         counter = counter<0 ? 0 : counter;
  25.  
  26.         if(counter){
  27.             console.log(`${action} x${counter}`);
  28.  
  29.             rock = operations['Transporting and washing'](rock);
  30.             console.log('Transporting and washing');
  31.         }
  32.  
  33.         return rock;
  34.     }
  35.  
  36.     for(chunk of givenChunks){
  37.         console.log(`Processing chunk ${chunk} microns`)
  38.  
  39.  
  40.         chunk = applyOperation('Cut', chunk);
  41.  
  42.         if(chunk==reqSize){
  43.             console.log(`Finished crystal ${chunk} microns`);
  44.             continue;
  45.         }
  46.  
  47.  
  48.         chunk = applyOperation('Lap', chunk);
  49.  
  50.         if(chunk==reqSize){
  51.             console.log(`Finished crystal ${chunk} microns`);
  52.             continue;
  53.         }
  54.  
  55.  
  56.         chunk = applyOperation('Grind', chunk);
  57.  
  58.         if(chunk==reqSize){
  59.             console.log(`Finished crystal ${chunk} microns`);
  60.             continue;
  61.         }
  62.  
  63.  
  64.         let counter = 0;
  65.         while(chunk>reqSize){
  66.             chunk = operations['Etch'](chunk);
  67.             counter++;
  68.         }
  69.  
  70.         if(counter){
  71.             console.log(`Etch x${counter}`);
  72.  
  73.             chunk = operations['Transporting and washing'](chunk);
  74.             console.log('Transporting and washing');
  75.         }
  76.  
  77.         if(chunk+1 == reqSize){
  78.             chunk = operations['X-ray'](chunk);
  79.             console.log(`X-ray x1`);
  80.         }
  81.  
  82.         console.log(`Finished crystal ${chunk} microns`);
  83.     }
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement