call23re

Advent Of Code - Day 2

Dec 18th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var total = 0
  2. var totalribbon = 0
  3.  
  4. function calculate(l,w,h){
  5. var smallest = 0
  6.  
  7. if(l*w<=w*h){
  8. if(l*w<=h*l){
  9. var smallest = l*w
  10. }
  11. }
  12.  
  13. if(w*h<=l*w){
  14. if(w*h<=h*l){
  15. var smallest = w*h
  16. }
  17. }
  18.  
  19. if(h*l<=w*h){
  20. if(h*l<=l*w){
  21. var smallest = h*l
  22. }
  23. }
  24.  
  25. total = total + 2*l*w + 2*w*h + 2*h*l + smallest;
  26. }
  27.  
  28. function calculateribbon(l,w,h){
  29. l = Number(l)
  30. w = Number(w)
  31. h = Number(h)
  32. var base = l*w*h
  33. var smallest = 0
  34. var smallesttype = 'l'
  35. var second = 0
  36.  
  37. if(l<=w&&l<=h){
  38. smallest = l
  39. smallesttype = 'l'
  40. }
  41. if(w<=l&&w<=h){
  42. smallest = w
  43. smallesttype = 'w'
  44. }
  45. if(h<=w&&h<=l){
  46. smallest = h
  47. smallesttype = 'h'
  48. }
  49.  
  50. if('l'!=smallesttype){
  51. if('h'!=smallesttype&&l<=h){
  52. second = l
  53. }
  54. if('w'!=smallesttype&&l<=w){
  55. second = l
  56. }
  57. }
  58.  
  59. if('h'!=smallesttype){
  60. if('l'!=smallesttype&&h<=l){
  61. second = h
  62. }
  63. if('w'!=smallesttype&&h<=w){
  64. second = h
  65. }
  66. }
  67.  
  68. if('w'!=smallesttype){
  69. if('l'!=smallesttype&&w<=l){
  70. second = w
  71. }
  72. if('h'!=smallesttype&&w<=h){
  73. second = w
  74. }
  75. }
  76. ribbon = Number(smallest) + Number(smallest) + Number(second) + Number(second)
  77. //console.log(l+','+w+','+h+'|'+Number(smallest)+':'+Number(second))
  78. var needed = base + ribbon
  79. totalribbon = totalribbon + needed
  80. }
  81.  
  82. function fixString(string){
  83. var cur = 0
  84. var p1 = 0
  85. var p2 = 0
  86. var p3 = 0
  87. var split = string.split('x')
  88. for(var i = 0; i < split.length; i++){
  89. if(i == 0){
  90. p1 = split[i]
  91. }
  92. if(i == 1){
  93. p2 = split[i]
  94. }
  95. if(i == 2){
  96. p3 = split[i]
  97. }
  98. }
  99. calculate(p1,p2,p3)
  100. calculateribbon(p1,p2,p3)
  101. }
  102.  
  103. $.get('http://adventofcode.com/day/2/input').success(function(data){
  104. var split = data.split('\n')
  105. for(var i = 0; i < split.length; i++){
  106. fixString(split[i])
  107. }
  108. console.log('Total Wrapping Paper: ' + total + '\n' + 'Total Ribbon: ' + totalribbon)
  109. })
Advertisement
Add Comment
Please, Sign In to add comment