-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle02.js
77 lines (69 loc) · 1.82 KB
/
puzzle02.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
const fs = require("fs");
const files = fs.readFileSync("./puzzle02-input.txt", "utf8"); //Read the input
const arr = files.replace(/\r/g, "").trim().split("\n"); //remove all carriage returns and split by single line return
const arrSplited = arr.map((x) => {
return x.split(" "); //split the two Letters by space (" ")
});
//Part 1
//Store the score for each round in the resultArray array.
const resultArray = arrSplited.map((x) => {
let count = 0;
if (x[1] === "X") {
count = 1;
} else if (x[1] === "Y") {
count = 2;
} else if (x[1] === "Z") {
count = 3;
}
if (
(x[0] === "A" && x[1] === "Y") ||
(x[0] === "B" && x[1] === "Z") ||
(x[0] === "C" && x[1] === "X")
) {
count += 6;
} else if (
(x[0] === "A" && x[1] === "X") ||
(x[0] === "B" && x[1] === "Y") ||
(x[0] === "C" && x[1] === "Z")
) {
count += 3;
}
return count;
});
//Part 1 solution. Get the total score:
const totalScore = resultArray.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
}, 0);
console.log(totalScore);
//Part 2. Need to modify the resultArray based on the new rules
const resultArray2 = arrSplited.map((x) => {
let count = 0;
if (x[1] === "X") {
count = 0;
} else if (x[1] === "Y") {
count = 3;
} else if (x[1] === "Z") {
count = 6;
}
if (
(x[0] === "A" && x[1] === "Y") ||
(x[0] === "B" && x[1] === "X") ||
(x[0] === "C" && x[1] === "Z")
) {
count += 1;
} else if (
(x[0] === "A" && x[1] === "Z") ||
(x[0] === "B" && x[1] === "Y") ||
(x[0] === "C" && x[1] === "X")
) {
count += 2;
} else {
count += 3;
}
return count;
});
//Part 2 solution. Get the total score:
const totalScore2 = resultArray2.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
}, 0);
console.log(totalScore2);