forked from meldeleon/aoc_2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5_pt2.js
60 lines (56 loc) · 1.3 KB
/
day5_pt2.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
const { truncate } = require("fs")
const input = Array.from(
require("fs").readFileSync("day5_input.txt").toString().split("\r\n")
)
let count = 0
input.forEach((str) => {
if (checkRepeatingPair(str) && checkforSandwich(str)) {
count++
} else {
return
}
})
console.log(count)
//ch3ck f0r r3p34t1ng p41r
function checkRepeatingPair(str) {
let strArray = Array.from(str)
let pairArray = strArray.map((x, index) => {
if (index < strArray.length - 1) {
let pair = []
pair = [x, strArray[index + 1]]
return pair
}
})
let pairRepeatCount = 0
pairArray.forEach((pair, index) => {
for (y = index + 1; y < pairArray.length - 1; y++) {
console.log(pair, pairArray[y])
if (pairArray[y].toString() === pair.toString() && y !== index + 1) {
pairRepeatCount++
}
}
})
if (pairRepeatCount > 0) {
return true
} else {
return false
}
}
//check for a sandwich
function checkforSandwich(str) {
let strArray = Array.from(str)
let sandwichCount = 0
let pairArray = strArray.forEach((character, index) => {
console.log(character, strArray[index + 2])
if (character === strArray[index + 2]) {
sandwichCount++
} else {
//do nothing
}
})
if (sandwichCount > 0) {
return true
} else {
return false
}
}