-
Notifications
You must be signed in to change notification settings - Fork 0
/
2022-3.js
76 lines (68 loc) · 1.85 KB
/
2022-3.js
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
var fs = require('fs');
const file = fs.readFileSync('input2022-3.txt', 'utf8');
let lines = file.split('\n');
let sum = 0;
// convert char to int
const c = (item) => {
const char = item.charCodeAt(0);
if (char >= 65 && char <= 90) {
return char - 65 + 27;
} else if (char >= 97 && char <= 122) {
return char - 71 - 25;
}
}
const part1 = (sum, items) => {
for (let j = 0; j < items.length; j++) {
if (j < items.length / 2) {
// just count items in the first half, where index is item, and value is count
countArr[c(items[j])]++;
} else {
const value = c(items[j]);
if (countArr[value] > 0) {
sum += value;
// if found remove from count, so we don't count it again
countArr[value] = 0;
}
}
}
countArr = new Array(28*2).fill(0);
return sum;
}
const part2 = (sum, items, i, countArr) => {
for (let j = 0; j < items.length; j++) {
const value = c(items[j]);
// first line of group, just count all items
if (i % 3 === 0) {
countArr[value]++;
}
// second line of group, mark items that were found in first line with -1
else if (i % 3 === 1) {
if (countArr[value] > 0) {
countArr[value] = -1;
}
}
// if we found item that was common in first both lines, mark it with -2
else {
if (countArr[value] === -1) {
countArr[value] = -2;
}
}
}
// if we are at the end of group, count all items that were found in all 3 lines
if (i % 3 === 2) {
for (let j = 0; j < countArr.length; j++) {
if (countArr[j] === -2) {
sum += j;
}
countArr[j] = 0;
}
}
return sum;
}
let countArr = new Array(28*2).fill(0);
for (let i = 0; i < lines.length; i++) {
const items = lines[i].split('');
sum = part1(sum, items, countArr);
// sum = part2(sum, items, i, countArr);
}
console.log(sum)