-
Notifications
You must be signed in to change notification settings - Fork 0
/
day07.js
120 lines (102 loc) · 2.59 KB
/
day07.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const readFrom = require("./read-input");
const args = process.argv;
const input = readFrom(args);
if (!input) return;
const labels = [
"A",
"K",
"Q",
"J",
"T",
"9",
"8",
"7",
"6",
"5",
"4",
"3",
"2",
];
const labelsWithJoker = [
"A",
"K",
"Q",
"T",
"9",
"8",
"7",
"6",
"5",
"4",
"3",
"2",
"J",
];
const getTypeStrength = (hand, withJoker) => {
const cards = new Map();
const jokers = hand.reduce((js, c) => {
if (!withJoker || c !== "J") {
cards.set(c, (cards.get(c) || 0) + 1);
}
return withJoker && c === "J" ? js + 1 : js;
}, 0);
const [maxLabel, _maxOcc] = [...cards.entries()].reduce(
(max, card) => {
return card[1] > max[1] ? card : max;
},
[null, null]
);
cards.set(maxLabel, cards.get(maxLabel) + jokers);
if (cards.size === 1) return 7;
if (cards.size === 2) {
for (const [_label, occurences] of cards) {
if (occurences === 1 || occurences === 4) return 6;
if (occurences === 2 || occurences === 3) return 5;
}
}
if (cards.size === 3) {
for (const [_label, occurences] of cards) {
if (occurences === 3) return 4;
if (occurences === 2) return 3;
}
}
if (cards.size === 4) return 2;
return 1;
};
const sortHands = (a, b, withJoker) => {
const handA = a.split(" ")[0].split("");
const handB = b.split(" ")[0].split("");
const typeDiff =
getTypeStrength(handB, withJoker) - getTypeStrength(handA, withJoker);
if (typeDiff === 0) {
for (let i = 0; i < handA.length; i++) {
const aIndex = withJoker
? labelsWithJoker.indexOf(handA[i])
: labels.indexOf(handA[i]);
const bIndex = withJoker
? labelsWithJoker.indexOf(handB[i])
: labels.indexOf(handB[i]);
if (aIndex < bIndex) return -1;
if (aIndex > bIndex) return 1;
}
}
return typeDiff;
};
const partOneResult = input
.split("\n")
.sort((a, b) => sortHands(a, b, false))
.reverse()
.reduce((sum, line, i) => {
const bid = parseInt(line.split(" ")[1]);
return sum + bid * (i + 1);
}, 0);
console.log(partOneResult);
const partTwoResult = input
.split("\n")
.sort((a, b) => sortHands(a, b, true))
.reverse()
.reduce((sum, line, i) => {
const bid = parseInt(line.split(" ")[1]);
return sum + bid * (i + 1);
}, 0);
console.log(partTwoResult);