-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththree.js
More file actions
85 lines (67 loc) · 1.47 KB
/
Copy paththree.js
File metadata and controls
85 lines (67 loc) · 1.47 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const { readFile } = require('fs/promises');
const LETTERS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
const a = 97;
const z = 122;
const A = 65;
const Z = 90;
// 3
async function init() {
const input = await readFile('three.txt', 'utf8');
// const input = await readFile('three.test.txt', 'utf8');
const data = input.split('\n');
let score = 0;
let group = [];
data.forEach((d, i) => {
const j = i % 3;
if (j === 0) {
group = [];
}
group[j] = d;
if (j === 2) {
const shared = findShared(group);
console.log({ shared });
score += shared;
}
});
console.log({ score });
}
function findShared(group) {
const all = [];
group.forEach((g) => {
[...new Set(g)].forEach((d) => {
const val = value(d);
all[val] > 0 ? all[val]++ : (all[val] = 1);
});
});
return all.indexOf(3);
}
function findDupe(pack) {
const len = pack.length;
if (len % 2) {
console.log({ len });
}
const left = pack.substring(0, len / 2);
const right = pack.substring(len / 2);
if (left.length !== right.length) {
console.log('DIFFERENT LENGTHS');
throw new Error();
}
// console.log({ left, right });
const set = new Set(left);
let char;
right.split('').forEach((d) => {
if (set.has(d)) {
char = d;
}
});
return char;
}
function value(char) {
const code = char.charCodeAt(0);
if (code < 95) {
// uppercase
return code - 38;
}
return code - 96;
}
init();