-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.ts
45 lines (40 loc) · 1.41 KB
/
14.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
45
import {countCharacters, loadData} from '../shared/utils';
export type DataType = [string, Array<[string, string]>];
/* istanbul ignore next */
export const parse = async (): Promise<DataType> => {
const [template, rules] = (await loadData(2021, 14)).split('\n\n');
return [
template,
rules.split('\n').map(rule => rule.split(' -> ') as [string, string]),
];
};
export const step = (input: string, rules: Array<[string, string]>): string => {
let output = '';
for (let index = 0; index < input.length; index++) {
output += input.charAt(index);
const pair = input.substring(index, index + 2);
const rule = rules.find(r => r[0] === pair);
if (rule) {
output += rule[1];
}
}
return output;
};
export const part1 = ([input, rules]: DataType): number => {
let polymer = input;
for (let index = 0; index < 10; index++) {
polymer = step(polymer, rules);
}
const counts = countCharacters(polymer);
const sortedCounts = Object.values(counts).sort((a, b) => b - a);
return sortedCounts[0] - sortedCounts[sortedCounts.length - 1];
};
export const part2 = ([input, rules]: DataType): number => {
let polymer = input;
for (let index = 0; index < 40; index++) {
polymer = step(polymer, rules);
}
const counts = countCharacters(polymer);
const sortedCounts = Object.values(counts).sort((a, b) => b - a);
return sortedCounts[0] - sortedCounts[sortedCounts.length - 1];
};