-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.js
67 lines (52 loc) · 1.09 KB
/
day2.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
const fs = require("fs");
const _ = require("lodash");
const input = fs.readFileSync("./day2.txt", "utf-8");
const scoreForAction = (x) =>
({
X: 1,
Y: 2,
Z: 3,
}[x]);
const scoreForResult = (x) =>
({
X: 0,
Y: 3,
Z: 6,
}[x]);
const scoreResultForA = (x) =>
({
"A X": 3,
"A Y": 6,
"A Z": 0,
"B X": 0,
"B Y": 3,
"B Z": 6,
"C X": 6,
"C Y": 0,
"C Z": 3,
}[x]);
const actionFromGuide = (x) =>
({
"A X": "Z",
"A Y": "X",
"A Z": "Y",
"B X": "X",
"B Y": "Y",
"B Z": "Z",
"C X": "Y",
"C Y": "Z",
"C Z": "X",
}[x]);
const scoreForA = (x) => {
const action = x.split(" ")[1];
return scoreForAction(action) + scoreResultForA(x);
};
const scoreForB = (x) => {
const action = actionFromGuide(x);
const result = x.split(" ")[1];
return scoreForAction(action) + scoreForResult(result);
};
const solutionA = _.sum(input.split("\n").filter(Boolean).map(scoreForA));
console.log("A", solutionA);
const solutionB = _.sum(input.split("\n").filter(Boolean).map(scoreForB));
console.log("B", solutionB);