Guest User

Untitled

a guest
Nov 26th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. process.stdin.resume();
  2. process.stdin.setEncoding('utf8');
  3. var map=[];
  4. map[0] = Array.from('qwertyuiop')
  5. map[1] = Array.from('asdfghjkl')
  6. map[2] = Array.from('zxcvbnm')
  7. function isAdj(a,b){
  8. var posAR,posAC;
  9. [0,1,2].forEach(function(itemR,index,arr){
  10. if(map[itemR].indexOf(a)!=-1){
  11. posAR=itemR
  12. posAC=map[itemR].indexOf(a)
  13. }
  14. })
  15. var posBR,posBC;
  16. [0,1,2].forEach(function(itemR,index,arr){
  17. if(map[itemR].indexOf(b)!=-1){
  18. posBR=itemR
  19. posBC=map[itemR].indexOf(b)
  20. }
  21. })
  22. var q1=(Math.abs(posAR-posBR)<=1 && posAC-posBC==0)
  23. var q2=(Math.abs(posAC-posBC)<=1 && posAR-posBR==0)
  24. return q1 || q2
  25. }
  26. var arrLeft = Array.from('qwertasdfgzxcvb')
  27.  
  28. process.stdin.on('data', function (chunk) {
  29. var line = chunk.toString();
  30. var mistake = 0
  31. var arrInput = Array.from(line)
  32. var prevHand = -1 //0=left
  33. arrInput.forEach(function(item, index, arr){
  34. var currentHand
  35. if(index!=0 && isAdj(arr[index],arr[index-1])){
  36.  
  37. currentHand = prevHand
  38. if(arrLeft.indexOf(item)!=-1){//should be left
  39. if(currentHand==1){
  40. mistake++
  41. }
  42. }else{
  43. if(currentHand==0){
  44. mistake++
  45. }
  46. }
  47. }else{
  48. if(arrLeft.indexOf(item)!=-1){
  49. currentHand = 0
  50. }else{
  51. currentHand = 1
  52. }
  53. }
  54.  
  55. prevHand=currentHand
  56. });
  57. console.log(mistake)
  58. });
Add Comment
Please, Sign In to add comment