-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_seven_part_one.ts
112 lines (96 loc) · 3.41 KB
/
day_seven_part_one.ts
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
import * as fs from "fs";
enum HandValue{
NotSure,
High_Card,
One_Pair,
Two_Pair,
Three_Of_A_Kind,
Full_House,
Four_Of_A_Kind,
Five_Of_A_Kind
}
const cardValues = ['A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2'];
class Hand{
public hand: string[];
public bid: number;
constructor(string_repr: string, bid: number){
this.hand = string_repr.split("");
this.bid = bid;
}
public value() {
const occurrences = this.hand.reduce(function (acc: any, curr: any):any {
return acc[curr] ? ++acc[curr] : acc[curr] = 1, acc
}, {});
let possibilities: HandValue[] = [];
let sureHandValue: HandValue = HandValue.NotSure;
const sortedList = Object.keys(occurrences).sort((a,b) => occurrences[b] - occurrences[a]).forEach( e => {
switch (occurrences[e]){
case 5:
sureHandValue = HandValue.Five_Of_A_Kind;
return;
case 4:
sureHandValue = HandValue.Four_Of_A_Kind;
return;
case 3:
possibilities.push(HandValue.Three_Of_A_Kind);
possibilities.push(HandValue.Full_House);
case 2:
if (possibilities.includes(HandValue.Full_House)){
possibilities = possibilities.filter(e => e != HandValue.Full_House);
sureHandValue = HandValue.Full_House;
return;
}
possibilities.push(HandValue.Two_Pair);
possibilities.push(HandValue.One_Pair);
case 1:
sureHandValue = HandValue.High_Card;
return;
}
});
if (possibilities.length == 1){
sureHandValue = possibilities[0];
}else{
let found_two_pair = false;
possibilities.forEach( e => {
switch (e){
case HandValue.Two_Pair:
if (possibilities.filter( e => e == HandValue.One_Pair).length == 2){
sureHandValue = HandValue.Two_Pair;
found_two_pair = true;
}
}
})
if (!found_two_pair){
sureHandValue = HandValue.One_Pair;
}
}
return sureHandValue;
}
}
function main(){
const input = fs.readFileSync("./input/day_seven.txt");
const input_as_string: string = input.toString();
const hands: Hand[] = input_as_string.split("\n").map(line => {
const [hand, bid] = line.split(" ");
const newHand = new Hand(hand, parseInt(bid));
return newHand;
})
hands.sort( (a: Hand, b: Hand): number => {
const handValueDiff = a.value() - b.value();
if (handValueDiff != 0){
return handValueDiff;
}
for (let i = 0; i < 5; i++){
const a_Hand = a.hand[i];
const b_Hand = b.hand[i];
if (a_Hand != b_Hand){
return cardValues.indexOf(b_Hand) - cardValues.indexOf(a_Hand);
}
}
return 0;
})
const value = hands.map( (hand: Hand, index: number) => hand.bid * (index + 1));
const answer = value.reduce((total, value) => total + value);
console.log(answer);
}
main();