Skip to content

Commit

Permalink
feature: It Hangs in the Balance (part 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotacke committed Nov 8, 2019
1 parent 4bc588f commit 9740977
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
23 changes: 23 additions & 0 deletions day-24-it-hangs-in-the-balance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,28 @@ Of these, although `10 9 1` has the smallest quantum entanglement (`90`), the co

What is the **quantum entanglement** of the first group of packages in the ideal configuration?

## Part Two

That's weird... the sleigh still isn't balancing.

"Ho ho ho", Santa muses to himself. "I forgot the trunk".

Balance the sleigh again, but this time, separate the packages into **four groups** instead of three. The other constraints still apply.

Given the example packages above, this would be some of the new unique first groups, their quantum entanglements, and one way to divide the remaining packages:

```
11 4 (QE=44); 10 5; 9 3 2 1; 8 7
10 5 (QE=50); 11 4; 9 3 2 1; 8 7
9 5 1 (QE=45); 11 4; 10 3 2; 8 7
9 4 2 (QE=72); 11 3 1; 10 5; 8 7
9 3 2 1 (QE=54); 11 4; 10 5; 8 7
8 7 (QE=56); 11 4; 10 5; 9 3 2 1
```

Of these, there are three arrangements that put the minimum (two) number of packages in the first group: `11 4`, `10 5`, and `8 7`. Of these, `11 4` has the lowest quantum entanglement, and so it is selected.

Now, what is the **quantum entanglement** of the first group of packages in the ideal configuration?

## References
- https://adventofcode.com/2015/day/24
54 changes: 54 additions & 0 deletions day-24-it-hangs-in-the-balance/quantum2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const findCombinations = (packages = [], currentCombination = [], maximumWeight = Infinity, maximumPackages = Infinity) => {
const availablePackages = [...packages];
const currentWeight = currentCombination.reduce((a, b) => a + b, 0);

if (currentWeight === maximumWeight && currentCombination.length <= maximumPackages) {
return [currentCombination];
}

let combinations = [];

while (availablePackages.length) {
const weight = availablePackages.shift();

if (currentWeight + weight <= maximumWeight) {
const nextCombination = [...currentCombination, weight];
const otherCombinations = findCombinations([...availablePackages], nextCombination, maximumWeight, maximumPackages);

otherCombinations.forEach((combination) => combinations.push(combination));
}
}

return combinations;
};

const findSmallestGroupSize = (packages, maximumWeight) => {
let weight = 0;
let numberOfPackages = 0;

for (let i = 0; i < packages.length; i++) {
if (weight + packages[i] <= maximumWeight) {
weight += packages[i];
numberOfPackages++;
}
}

return numberOfPackages;
};

module.exports = (list) => {
const packages = list
.split('\n')
.map((item) => parseInt(item))
// sort package weights descending
.sort((a, b) => b - a);

const totalWeight = packages.reduce((a, b) => a + b, 0);
const maximumGroupWeight = totalWeight / 4;

const maximumPackages = findSmallestGroupSize(packages, maximumGroupWeight);

return findCombinations(packages, [], maximumGroupWeight, maximumPackages)
.map((group) => group.reduce((a, b) => a * b, 1))
.sort((a, b) => a - b)[0];
};
21 changes: 21 additions & 0 deletions day-24-it-hangs-in-the-balance/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const assert = require('assert');

const quantum = require('./quantum');
const quantum2 = require('./quantum2');

describe('Day 24: It Hangs in the Balance', () => {
it('should calculate quantum entanglement', () => {
Expand All @@ -20,4 +21,24 @@ describe('Day 24: It Hangs in the Balance', () => {

assert.strictEqual(entanglement, 99);
});

describe('Part Two', () => {
it('should calculate quantum entanglement with four compartments', () => {
const packages =
`1
2
3
4
5
7
8
9
10
11`;

const entanglement = quantum2(packages);

assert.strictEqual(entanglement, 44);
});
});
});

0 comments on commit 9740977

Please sign in to comment.