-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.js
94 lines (75 loc) · 2.23 KB
/
part1.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const { getInputString } = require("../utils/getInputString.js");
const sum = arr => arr.reduce((acc, cur) => {
acc += cur;
return acc;
}, 0);
const getPairs = (file) => {
const pairs = [];
let currentPair = [];
for(const line of file.split('\n')) {
if(line === "") {
pairs.push(currentPair);
currentPair = [];
} else {
currentPair.push(JSON.parse(line));
}
}
pairs.push(currentPair);
return pairs
}
const results = {
continue: 'c',
failed: 'f',
passed: 'p'
}
const comparePair = (left, right) => {
console.log(left, right)
const loops = Math.max(left.length, right.length);
for(let i = 0; i < loops; i++) {
let result = results.continue;
const leftItem = left[i]
const rightItem = right[i]
console.log(`compare ${leftItem} vs ${rightItem}`)
if(leftItem === undefined) {
console.log('left side ran out of items so inputs are in the right order')
return results.passed;
}
if(rightItem === undefined) {
console.log('right side ran out of items so inputs are not in the right order')
return results.failed;
}
if(Array.isArray(leftItem) && Array.isArray(rightItem)) {
result = comparePair(leftItem, rightItem)
} else if(Array.isArray(leftItem) && !Array.isArray(rightItem)) {
result = comparePair(leftItem, [rightItem]);
} else if(!Array.isArray(leftItem) && Array.isArray(rightItem)) {
result = comparePair([leftItem], rightItem);
} else if(leftItem > rightItem) {
console.log('right side is smaller, so inputs are not in the right order')
result = results.failed;
} else if (leftItem < rightItem) {
console.log('left side is smaller, so inputs are in the right order')
result = results.passed;
}
if(result === results.failed) {
return results.failed;
}
if(result === results.passed) {
return results.passed;
}
}
}
const Run = () => {
const file = getInputString(13, false);
const pairs = getPairs(file);
const indexes = [];
for(const i in pairs) {
const pair = pairs[i];
const result = comparePair(pair[0], pair[1]);
if(result === results.passed) {
indexes.push(parseInt(i)+1);
}
}
console.log(sum(indexes));
}
Run();