-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8.js
49 lines (47 loc) · 1004 Bytes
/
day8.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
// problem: http://adventofcode.com/day/8
// input: http://adventofcode.com/day/8/input
function parseLine(s) {
if (!s) {
return 0;
}
var skipped = 2; // assume quotes
var i = 1;
while (i < s.length-2) {
if (s[i] === '\\') {
if (s[i+1] === '\"' || s[i+1] === '\\') {
skipped++;
i += 2;
} else if (s[i+1] === 'x') {
// assume two hex chars follow
skipped += 3;
i += 4;
}
} else {
i++;
}
}
return skipped;
}
var sum = 0;
var input = document.body.innerText.split('\n');
for (var j = 0; j < input.length; j++) {
sum += parseLine(input[j]);
}
console.log(sum);
function embiggenLine(s) {
if (!s) {
return 0;
}
var added = 2; // surround line in double-quotes
for (var k = 0; k < s.length; k++) {
if (s[k] === '\\' || s[k] === '\"') {
added++;
}
}
return added;
}
var sum2 = 0;
for (var j = 0; j < input.length; j++) {
sum2 += embiggenLine(input[j]);
}
console.log(sum2);