Skip to content

Commit 0eb206b

Browse files
committed
feat: Day 5: Alchemical Reduction (part 2)
1 parent da91118 commit 0eb206b

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

day-05-alchemical-reduction/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,22 @@ After all possible reactions, the resulting polymer contains **10 units**.
2626

2727
**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.)*
2828

29+
## Part Two
30+
31+
Time to improve the polymer.
32+
33+
One of the unit types is causing problems; it's preventing the polymer from collapsing as much as it should. Your goal is to figure out which unit type is causing the most problems, remove all instances of it (regardless of polarity), fully react the remaining polymer, and measure its length.
34+
35+
For example, again using the polymer `dabAcCaCBAcCcaDA` from above:
36+
37+
- Removing all `A`/`a` units produces `dbcCCBcCcD`. Fully reacting this polymer produces `dbCBcD`, which has length 6.
38+
- Removing all `B`/`b` units produces `daAcCaCAcCcaDA`. Fully reacting this polymer produces `daCAcaDA`, which has length 8.
39+
- Removing all `C`/`c` units produces `dabAaBAaDA`. Fully reacting this polymer produces `daDA`, which has length 4.
40+
- Removing all `D`/`d` units produces `abAcCaCBAcCcaA`. Fully reacting this polymer produces `abCBAc`, which has length 6.
41+
42+
In this example, removing all `C`/`c` units was best, producing the answer **4**.
43+
44+
**What is the length of the shortest polymer you can produce** by removing all units of exactly one type and fully reacting the result?
45+
2946
## References
3047
- https://adventofcode.com/2018/day/5
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const react = (polymer) => {
2+
let hasReaction = true;
3+
let chain = polymer;
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+
const polymer = (input) => {
28+
return Array
29+
.from({ length: 26 })
30+
.map((_, i) => input
31+
.replace(new RegExp(String.fromCharCode(i + 97), 'g'), '')
32+
.replace(new RegExp(String.fromCharCode(i + 65), 'g'), ''))
33+
.map((x) => react(x))
34+
.sort((a, b) => a - b)[0];
35+
};
36+
37+
module.exports = polymer;

day-05-alchemical-reduction/test.js

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const assert = require('assert');
22

33
const polymer = require('./polymer');
4+
const polymer2 = require('./polymer2');
45

56
describe('Day 5: Alchemical Reduction', () => {
67
it('should calculate polymer from aA', () => {
@@ -22,4 +23,10 @@ describe('Day 5: Alchemical Reduction', () => {
2223
it('should calculate polymer from dabAcCaCBAcCcaDA', () => {
2324
assert.strictEqual(polymer('dabAcCaCBAcCcaDA'), 10);
2425
});
26+
27+
describe('Part Two', () => {
28+
it('should calculate polymer from dabAcCaCBAcCcaDA', () => {
29+
assert.strictEqual(polymer2('dabAcCaCBAcCcaDA'), 4);
30+
});
31+
});
2532
});

0 commit comments

Comments
 (0)