Skip to content

Instantly share code, notes, and snippets.

@g-cassie
Last active December 2, 2015 18:38
Show Gist options
  • Save g-cassie/06a14783eaf6167b00cc to your computer and use it in GitHub Desktop.
Save g-cassie/06a14783eaf6167b00cc to your computer and use it in GitHub Desktop.
Advent of Code Day 2 - Ember Solver
import Ember from 'ember';
const BoxToWrap = Ember.Object.extend({
length: 0,
height: 0,
width: 0,
sides: Ember.computed('length', 'width', 'height', function(){
const dims = ['length', 'width', 'height'];
const sides = [];
dims.forEach(dim => {
let newSides = dims.filter(otherDim => dim != otherDim).map(
otherDim => this.get(dim) * this.get(otherDim)
);
sides.pushObjects(newSides);
});
return sides;
}),
smallestSide: Ember.computed.min('sides'),
totalSurfaceArea: Ember.computed('sides', 'smallestSide', function(){
let result = 0;
this.get('sides').forEach(x => result += x);
result += this.get('smallestSide');
return result;
})
});
export default Ember.Controller.extend({
appName:'Ember Twiddle',
input: '',
boxes: Ember.computed('input', function(){
const dims = this.get('input').split('\n');
return !!dims ? dims.map(dim => {
let [length, height, width] = dim.split('x');
return BoxToWrap.create({length, height, width});
}) : [];
}),
totalPaperRequired: Ember.computed('boxes', function(){
let x = 0;
this.get('boxes').forEach(b => x += b.get('totalSurfaceArea'));
return x;
})
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
Paste Dimensions Here:
{{textarea value=input}}
<br>
You need {{totalPaperRequired}} square feet of wrapping paper.
{
"version": "0.4.16",
"EmberENV": {
"FEATURES": {}
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.1.0/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.1.0/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.1.0/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment