-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05.ts
44 lines (39 loc) · 1.49 KB
/
05.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { L } from "../../common/language.ts";
const isOppositePolarity = (a: string, b: string): boolean =>
a !== b && a.toLocaleLowerCase() === b.toLocaleLowerCase();
// Remove all neighbouring characters with opposite polarity,
// until the polymer is stable (i.e. no aA, bB, cC... pairs)
// dabAcCaCBAcCcaDA -> dabCBAcaDA
const getCollapsedPolymerLength = (polymer: string): number => {
const stack: string[] = [];
for (const unit of polymer) {
const prev = stack[stack.length - 1];
if (prev && isOppositePolarity(unit, prev)) {
stack.pop();
continue;
}
stack.push(unit);
}
return stack.length;
};
// Get the lengths of the collapsed polymers, when all instances
// of each different unit type are removed before collapsing
// dabAcCaCBAcCcaDA -> remove a/A -> dbcCCBcCcD -> dbCBcD -> 6
// dabAcCaCBAcCcaDA -> remove b/B -> daAcCaCAcCcaDA -> daCAcaDA -> 8
const getModifiedPolymerLengths = (input: string): number[] =>
L.alphabet.split("").map((char) =>
getCollapsedPolymerLength(
input
.replaceAll(char, "")
.replaceAll(char.toUpperCase(), ""),
)
);
// "How many units remain after fully reacting the polymer you scanned?"
export function part1(input: string) {
return getCollapsedPolymerLength(input);
}
// "What is the length of the shortest polymer you can produce by removing
// all units of exactly one type and fully reacting the result?"
export function part2(input: string) {
return Math.min(...getModifiedPolymerLengths(input));
}