-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path2.js
executable file
·57 lines (52 loc) · 1.27 KB
/
2.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
#!/usr/bin/env nodejs
const runme = () => {
let fs = require('fs')
let contents = fs.readFileSync('./input.txt', 'utf8')
let lines = contents.split(/\n/)
let twoCount = 0
let threeCount = 0
for (let line of lines) {
const hasTwo = hasExactCount(line, 2)
const hasThree = hasExactCount(line, 3)
if (hasTwo) { twoCount += 1 }
if (hasThree) { threeCount += 1 }
}
console.log("Checksum: ");
console.log(twoCount * threeCount + "\n")
for (let line1 of lines) {
for (let line2 of lines) {
if (areLinesOneOff(line1, line2)) {
return
}
}
}
}
const areLinesOneOff = (a, b) => {
let differences = 0
let diff_index = 0
for (let i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
differences += 1
diff_index = i
}
}
if (differences == 1) {
console.log('found it!')
console.log('------------')
console.log(a)
console.log(b)
const inCommon = a.slice(0, diff_index) + a.slice(diff_index + 1)
console.log(inCommon);
console.log('------------')
return true
}
return false
}
const hasExactCount = (line, count) => {
let seen = {}
for (let char of line) {
seen[char] = seen[char] != null ? seen[char] + 1 : 1
}
return Object.values(seen).some(x => x === count)
}
runme()