Skip to content

Commit da91118

Browse files
committed
feat: Day 5: Alchemical Reduction (part 1)
1 parent 366e158 commit da91118

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
1515
- [Day 2: Inventory Management System](day-02-inventory-management-system/)
1616
- [Day 3: No Matter How You Slice It](day-03-no-matter-how-you-slice-it/)
1717
- [Day 4: Repose Record](day-04-repose-record/)
18+
- [Day 5: Alchemical Reduction](day-05-alchemical-reduction/)
1819

1920
## Running Tests
2021

day-05-alchemical-reduction/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Day 5: Alchemical Reduction
2+
3+
You've managed to sneak in to the prototype suit manufacturing lab. The Elves are making decent progress, but are still struggling with the suit's size reduction capabilities.
4+
5+
While the very latest in 1518 alchemical technology might have solved their problem eventually, you can do better. You scan the chemical composition of the suit's material and discover that it is formed by extremely long [polymers](https://en.wikipedia.org/wiki/Polymer) (one of which is available as your puzzle input).
6+
7+
The polymer is formed by smaller **units** which, when triggered, react with each other such that two adjacent units of the same type and opposite polarity are destroyed. Units' types are represented by letters; units' polarity is represented by capitalization. For instance, `r` and `R` are units with the same type but opposite polarity, whereas `r` and `s` are entirely different types and do not react.
8+
9+
For example:
10+
11+
- In `aA`, `a` and `A` react, leaving nothing behind.
12+
- In `abBA`, `bB` destroys itself, leaving `aA`. As above, this then destroys itself, leaving nothing.
13+
- In `abAB`, no two adjacent units are of the same type, and so nothing happens.
14+
- In `aabAAB`, even though `aa` and `AA` are of the same type, their polarities match, and so nothing happens.
15+
16+
Now, consider a larger example, `dabAcCaCBAcCcaDA`:
17+
18+
```
19+
dabAcCaCBAcCcaDA The first 'cC' is removed.
20+
dabAaCBAcCcaDA This creates 'Aa', which is removed.
21+
dabCBAcCcaDA Either 'cC' or 'Cc' are removed (the result is the same).
22+
dabCBAcaDA No further actions can be taken.
23+
```
24+
25+
After all possible reactions, the resulting polymer contains **10 units**.
26+
27+
**How many units remain after fully reacting the polymer you scanned?** *(Note: in this puzzle and others, the input is large; if you copy/paste your input, make sure you get the whole thing.)*
28+
29+
## References
30+
- https://adventofcode.com/2018/day/5
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const polymer = (input) => {
2+
let hasReaction = true;
3+
let chain = input;
4+
5+
restart:
6+
while (hasReaction) {
7+
hasReaction = false;
8+
9+
for (let i = 0; i < chain.length; i++) {
10+
const pair = chain.slice(i, i + 2);
11+
12+
if (pair.length === 2 &&
13+
(pair.charCodeAt(0) === pair.charCodeAt(1) + 32) ||
14+
pair.charCodeAt(0) === pair.charCodeAt(1) - 32) {
15+
16+
chain = chain.slice(0, i) + chain.slice(i + 2, chain.length);
17+
hasReaction = true;
18+
19+
continue restart;
20+
}
21+
}
22+
}
23+
24+
return chain.length;
25+
};
26+
27+
module.exports = polymer;

day-05-alchemical-reduction/test.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const assert = require('assert');
2+
3+
const polymer = require('./polymer');
4+
5+
describe('Day 5: Alchemical Reduction', () => {
6+
it('should calculate polymer from aA', () => {
7+
assert.strictEqual(polymer('aA'), 0);
8+
});
9+
10+
it('should calculate polymer from abBA', () => {
11+
assert.strictEqual(polymer('abBA'), 0);
12+
});
13+
14+
it('should calculate polymer from abAB', () => {
15+
assert.strictEqual(polymer('abAB'), 4);
16+
});
17+
18+
it('should calculate polymer from aabAAB', () => {
19+
assert.strictEqual(polymer('aabAAB'), 6);
20+
});
21+
22+
it('should calculate polymer from dabAcCaCBAcCcaDA', () => {
23+
assert.strictEqual(polymer('dabAcCaCBAcCcaDA'), 10);
24+
});
25+
});

0 commit comments

Comments
 (0)