-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.js
99 lines (81 loc) · 2.29 KB
/
day9.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
95
96
97
98
99
const fs = require('fs')
fs.readFile('./input_day9.txt', 'utf8' , (err, data) => {
if (err) {
console.error(err)
return err;
}
// calculateSeatIDs("BFFFBBFRRR");
// calculateSeatIDs("FFFBBBFRRR");
// calculateSeatIDs("BBFFBBFRLL");
let start1 = new Date().getTime();
decryptData(data);
let end1 = new Date().getTime();
let start2 = new Date().getTime();
decryptAgain(data);
let end2 = new Date().getTime();
console.log("Part 1:", end1 - start1);
console.log("Part 2:", end2 - start2);
})
function decryptAgain(input) {
let rows = input.split("\n");
rows.splice(rows.length - 1, 1);
let target = 26796446;
let foundRange = false;
for (var i = 0; i < rows.length; i++) {
let range = [];
for (var j = i ; j < rows.length; j++) {
range.push(rows[j]);
let sum = 0;
for (var k = 0; k < range.length; k++) {
sum += parseInt(range[k]);
}
if (sum > target) {
j++;
}
if (sum === target) {
console.log("FOUND");
range.sort();
console.log(range.length, range, sum, target);
console.log(parseInt(range[0]) + parseInt(range[range.length - 1]));
foundRange = true;
break;
}
}
if (foundRange) { break; }
}
}
function decryptData(input) {
let rows = input.split("\n");
rows.splice(rows.length - 1, 1);
rows.forEach((row, i) => {
if (i <= 24) {
console.log(i, row);
} else {
let lastNumbers = [];
for (var j = 0; j < 25; j++) {
lastNumbers[j] = parseInt(rows[i - 25 + j]);
}
//console.log(lastNumbers);
//let valid = determineValid(lastNumbers, parseInt(row));
console.log(lastNumbers.length, determineValid(lastNumbers, parseInt(row)), row);
}
});
}
function determineValid(previousNumbers, target) {
let valid = false;
for (var i = 0; i < previousNumbers.length; i++) {
for (var j = 0; j < previousNumbers.length; j++) {
if (i !== j) {
//console.log(previousNumbers[i], previousNumbers[j], previousNumbers[i] + previousNumbers[j], target);
if (previousNumbers[i] + previousNumbers[j] === target) {
valid = true;
break;
}
}
}
if (valid) {
break;
}
}
return [valid, previousNumbers[i], previousNumbers[j], i, j];
}