-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.js
69 lines (59 loc) · 2.01 KB
/
day7.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
import { asLines } from './util.js';
const input = asLines('../input/day7.txt');
const hands = input.map(line => line.split(' '))
const countCards = (hand) => { // build up a map of card kinds to counts
return Array.from(hand).reduce((acc, cur) => {
acc[cur] ? acc[cur]++ : acc[cur] = 1;
return acc;
}, {});
}
const cardScore = 'AKQJT98765432'.split('');
const jokerScore = 'AKQT98765432J'.split('');
const cardScorer = (cardIndex) => (card) => cardIndex.length - cardIndex.indexOf(card);
const handScorer = (useJokers) => (hand) => {
const cards = countCards(hand);
let jokers = 0;
if (useJokers) {
jokers = cards['J'] ?? 0;
delete cards['J'];
}
const kindCounts = Object.keys(cards).length;
const maxKind = Math.max(...Object.values(cards)) + jokers;
if (kindCounts <= 1) {
return 6; // five of a kind
} else if (kindCounts === 2 && maxKind === 4) {
return 5; // four of a kind
} else if (kindCounts === 2) {
return 4; // full house
} else if (maxKind === 3) {
return 3; // three of a kind
} else if (kindCounts === 3 && maxKind === 2) {
return 2; // two pair
} else if (maxKind === 2) {
return 1; // one pair
}
return 0; // high card
}
const handSorter = (handScorer, cardScorer) => (a, b) => {
if (handScorer(a) !== handScorer(b)) {
return handScorer(a) - handScorer(b);
}
// same hand, sort by card
for (let i = 0; i < a.length; i++) {
if (cardScorer(a[i]) !== cardScorer(b[i])) {
return cardScorer(a[i]) - cardScorer(b[i]);
}
}
return 0;
}
const winnings = (handSorter) => {
const sorted = hands.sort((a, b) => handSorter(a[0], b[0]));
return sorted.map((hand, index) => hand[1] * (index+1))
.reduce((a, b) => a + b, 0);
}
const part1Sorter = handSorter(handScorer(false), cardScorer(cardScore));
const winnings1 = winnings(part1Sorter);
console.log(`part 1: ${winnings1}`);
const part2Sorter = handSorter(handScorer(true), cardScorer(jokerScore));
const winnings2 = winnings(part2Sorter);
console.log(`part 2: ${winnings2}`);