Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var total = 0
- var totalribbon = 0
- function calculate(l,w,h){
- var smallest = 0
- if(l*w<=w*h){
- if(l*w<=h*l){
- var smallest = l*w
- }
- }
- if(w*h<=l*w){
- if(w*h<=h*l){
- var smallest = w*h
- }
- }
- if(h*l<=w*h){
- if(h*l<=l*w){
- var smallest = h*l
- }
- }
- total = total + 2*l*w + 2*w*h + 2*h*l + smallest;
- }
- function calculateribbon(l,w,h){
- l = Number(l)
- w = Number(w)
- h = Number(h)
- var base = l*w*h
- var smallest = 0
- var smallesttype = 'l'
- var second = 0
- if(l<=w&&l<=h){
- smallest = l
- smallesttype = 'l'
- }
- if(w<=l&&w<=h){
- smallest = w
- smallesttype = 'w'
- }
- if(h<=w&&h<=l){
- smallest = h
- smallesttype = 'h'
- }
- if('l'!=smallesttype){
- if('h'!=smallesttype&&l<=h){
- second = l
- }
- if('w'!=smallesttype&&l<=w){
- second = l
- }
- }
- if('h'!=smallesttype){
- if('l'!=smallesttype&&h<=l){
- second = h
- }
- if('w'!=smallesttype&&h<=w){
- second = h
- }
- }
- if('w'!=smallesttype){
- if('l'!=smallesttype&&w<=l){
- second = w
- }
- if('h'!=smallesttype&&w<=h){
- second = w
- }
- }
- ribbon = Number(smallest) + Number(smallest) + Number(second) + Number(second)
- //console.log(l+','+w+','+h+'|'+Number(smallest)+':'+Number(second))
- var needed = base + ribbon
- totalribbon = totalribbon + needed
- }
- function fixString(string){
- var cur = 0
- var p1 = 0
- var p2 = 0
- var p3 = 0
- var split = string.split('x')
- for(var i = 0; i < split.length; i++){
- if(i == 0){
- p1 = split[i]
- }
- if(i == 1){
- p2 = split[i]
- }
- if(i == 2){
- p3 = split[i]
- }
- }
- calculate(p1,p2,p3)
- calculateribbon(p1,p2,p3)
- }
- $.get('http://adventofcode.com/day/2/input').success(function(data){
- var split = data.split('\n')
- for(var i = 0; i < split.length; i++){
- fixString(split[i])
- }
- console.log('Total Wrapping Paper: ' + total + '\n' + 'Total Ribbon: ' + totalribbon)
- })
Advertisement
Add Comment
Please, Sign In to add comment