package nl.meine.aoc._2023

class Day7 {

    fun one(input: String): Int {
        val lines = createHands(input)

        val h = orderHands(lines)
        h.forEach { println(it) }

        return h.mapIndexed { rank, hand -> (rank + 1) * hand.bid }
            .sum()
    }

    fun createHands(input: String): MutableList<Hand> {
        return input.split("\n")
            .map { it.split(" ") }
            .map { Hand(it[0].chunked(1).map { c -> Card.getCard(c) }.toList(), it[1].toInt()) }
            .map { processJokers(it) }
            .toMutableList()
    }

    fun orderHands(hand: MutableList<Hand>): MutableList<Hand> {
        hand.sort()
        hand.reversed()
        return hand

    }

    fun processJokers(hand: Hand): Hand {
        val initial = hand.hand.filter { it.name != "J" }.groupingBy { it }.eachCount().toMutableMap()
        if (initial.isEmpty()) {
            initial[Card.A] = 0
        }

        val a = initial.maxBy { it.value }.key
        initial[a] = initial[a]!! + hand.hand.count { it.name == "J" }

        hand.counts = initial

        return hand
    }

    fun two(input: String): Int {
        val lines = createHands(input)

        val h = orderHands(lines)
        h.forEach { println(it) }

        return h.mapIndexed { rank, hand -> (rank + 1) * hand.bid }
            .sum()
    }

}


// two: 250665479
  //      250665248
fun main() {
    val inputReal =
        """9A35J 469
75T32 237
6T8JQ 427
3366A 814
K2AK9 982
J8KTT 9
94936 970
Q8AK9 15
3QQ32 940
65555 484
8K88K 674
Q67T5 788
77575 476
KAKAA 785
AA3AA 240
44767 423
Q923A 300
KK444 650
QQQ6Q 313
5JA22 167
7A264 837
TTKTT 646
K62JJ 682
34A63 532
7J554 393
Q86T6 745
9963K 718
92K85 97
3KKJ3 604
98Q26 257
7AJ6Q 132
K48T5 125
554J4 408
T6333 178
5757J 479
8J222 488
KKJ6A 460
95T38 944
23J22 796
AK333 69
25AK9 978
77779 123
K9T83 56
46A39 730
655J5 77
A2T2A 78
J8Q75 847
QA82A 700
TTT67 443
KT9A5 343
6969T 582
A7779 703
3QA93 597
6JT6T 558
28888 290
26TT2 147
T9356 587
6T542 482
3J733 140
TQT72 534
3K3K3 208
5JA6J 833
64646 277
86K68 795
24455 997
3633K 504
K3K36 790
22KK2 731
AA8AA 991
A6666 312
38395 712
46J44 229
52529 268
49AA9 573
J7J94 258
87TKA 947
J2999 555
T9KJK 194
7KK3K 146
T255T 875
9QAT4 475
759A2 169
22322 510
27T69 299
644K6 4
44J84 227
6A722 806
775KJ 25
37425 679
7TA8T 226
3Q6TK 391
8T3J4 196
67534 339
5QQ55 565
J33A3 305
8A8AJ 732
2QQJ5 523
AAAJA 618
97733 858
2A6Q2 433
J8QT3 390
3333J 910
K2AK2 884
AQTTQ 810
55666 855
7Q7QJ 743
K8KK3 489
4AKKJ 619
QQKQQ 373
QJ59A 688
2687J 622
7Q77J 512
578A3 120
T2662 737
JKJK7 807
44T94 223
454Q5 607
75J58 551
A686J 763
TAT4T 108
75356 591
96989 977
K7KTK 775
5252Q 318
77A5A 599
22K87 359
JJQQQ 930
8TKT8 436
A9QQ8 374
4T2JA 30
777AJ 463
8J888 87
42K8Q 657
J5JJ5 713
AKQJ3 45
T59K4 765
22A77 266
K8KK8 219
6T666 349
72T7T 92
3JQJQ 964
AA2J9 672
66997 803
5A8A2 467
KKKQ7 98
67Q5J 669
J57A6 503
98347 331
6J668 279
9K7KK 319
84848 337
33353 974
A333T 576
66636 461
6AAA2 281
Q52QQ 691
7876A 892
533T8 966
5A9Q7 533
A6AAQ 21
688Q6 100
K8483 764
62865 453
39323 189
TTJJT 498
5J437 306
55A7A 876
9A366 562
8J479 249
65544 640
83T88 325
AK45A 321
39QQ3 849
83838 749
43394 602
33722 843
T5KTJ 428
JJKK9 549
33737 27
4KKK4 638
KK398 246
J6828 372
99229 23
2963K 577
3ATJJ 862
6488T 912
48554 903
3A353 627
86923 768
79A98 540
77TTT 965
TK2T7 188
2A644 687
QT943 253
K2QJT 887
J5KK5 915
9Q2JA 980
J237J 28
A46Q2 264
7T2TT 621
A4T8J 57
Q5QT5 217
K9AK7 248
TAJTT 973
82822 919
858AA 265
5555J 800
552J2 908
Q5248 272
A5KK5 529
K8J9T 85
888TT 477
665Q5 823
Q42AT 449
4843A 83
Q5476 885
Q52T2 409
6A2TJ 580
92A99 483
Q9Q6Q 734
36662 134
88899 401
KQQQ9 389
JQ259 658
57895 639
KQ654 852
94273 612
77KT7 987
3Q4AQ 364
6KQK6 651
4AQ23 163
34TTT 107
T98T2 870
J78Q9 756
3K3J3 404
J993J 328
Q8JQA 678
96759 326
2J22J 383
2T222 236
76666 772
QQ5QJ 225
A47AA 505
967TJ 924
Q3TJ4 63
2KK29 879
44Q4Q 685
TTQ9T 659
7244J 414
T22TJ 269
55552 3
J78TK 474
3J3J3 559
8QJ42 382
28T86 740
TKTT8 381
22J44 579
J8668 242
88TT6 709
KKKJ9 192
478K2 501
3Q66J 22
K9KAJ 774
K3333 113
4TJT4 945
Q55KK 609
Q7272 955
6AKAA 490
964T5 971
36Q3Q 51
3T9AT 969
Q8TQT 256
4A833 632
5499J 812
44222 176
6QQ6Q 317
TJT2T 206
A862Q 468
36663 464
54JK9 302
8J828 103
32QQ2 38
66675 595
2J8TQ 50
2QQQQ 783
333QQ 282
67KKK 511
2996J 518
J824K 44
K6656 726
Q7Q5T 793
KQ27T 494
T2KQA 308
AAAA2 441
24J49 716
K58TT 873
7TA49 39
6KQ7T 298
6Q555 455
66AJA 819
5A9A3 922
AAA88 170
42324 962
22Q22 616
44QJQ 399
9J3TA 342
56K3A 588
99T9A 224
56T66 869
77997 671
53222 161
8KKJT 384
37777 567
TKTK5 42
6J622 127
387Q5 417
T3A73 360
28AKA 868
K26Q5 288
J6J88 692
KKKK9 690
2A5QA 435
K83T4 158
QAA3A 104
T7Q66 914
A3A6A 270
J888J 838
3JTKQ 867
7KKKK 451
K7766 824
Q4Q9Q 620
65A33 957
99A9A 781
A9AAA 344
44744 935
6666J 804
A8QJ9 155
AQKAA 681
6K7KJ 853
A99JA 366
65JJ5 47
4TQ8T 769
66555 457
TJ226 222
78777 758
5K488 999
Q65T4 362
4Q738 80
5JJ55 323
8Q65A 918
A5TT5 353
294J3 471
33Q44 102
66668 254
7663Q 516
AAAJJ 798
3344J 10
85888 507
68868 144
588T5 124
TT4TT 834
QQ7QK 168
75555 593
849KA 446
9QT7A 165
5AA7A 697
J99J9 213
6834K 5
TQQQT 292
7A655 403
982AQ 143
A6237 334
QK5T6 963
99895 232
56366 376
34J35 231
AAQAQ 150
78887 485
QQQQJ 263
TAQ98 197
425J5 895
JQ7QQ 992
J9JJ9 61
5888J 665
6J6K6 496
78J82 293
22292 73
TT2TT 911
ATAAT 322
339AA 953
A7465 413
AA4AA 283
QT3TQ 84
QQA5Q 66
J472J 784
T3JTT 131
333A4 210
79299 550
88A8J 594
J9749 584
J735K 902
933JQ 109
5T926 481
4T4T4 561
87TK6 247
98964 159
78997 354
36JT4 29
59464 771
2AA5A 33
77J43 244
37T7T 448
23QJ3 925
K7K5Q 792
QQQQ9 411
KK568 278
49TK7 961
55TAA 817
33J4Q 984
5QJK9 105
9A9J9 623
AK62Q 842
38333 735
9A8J8 333
9T243 204
6K2J3 547
78JKQ 959
AA3A9 634
KJJKT 13
KJKKJ 921
99934 310
4J929 431
55559 445
TT62T 355
32K25 825
622A6 480
334Q9 412
26922 114
638KJ 900
364T8 462
QQJT7 234
TK586 525
K342K 816
TTT99 699
JT658 315
36388 24
2666J 185
53455 592
4AJAA 304
J3QQ8 228
22J2A 416
99768 927
TKJ9A 487
5A55J 744
QKQ44 613
336QA 81
66622 200
JTTTK 717
59ATT 513
33938 813
66AKJ 466
3976Q 941
8888T 491
JJK47 836
99Q7Q 654
Q896K 633
Q8JKQ 553
J6K6K 811
2T323 750
KKAKK 952
Q6JJ4 929
77J77 993
9KQJT 989
44254 857
4AA5A 514
TATJA 715
74422 145
33332 544
5T5QT 311
78JT8 913
JKKT2 94
26QK7 71
444JJ 287
99979 133
Q999Q 314
48888 649
33T8K 368
77T57 711
4T42Q 351
89Q99 89
69666 846
3A33A 890
8Q8A3 636
4A888 603
9TQTA 747
9AAA9 52
TJ4TT 267
J76J7 72
K773Q 370
77Q77 329
636QQ 575
ATQAA 917
2743Q 932
2KKKJ 452
93929 686
4ATTA 458
42222 252
35225 402
45JKA 160
4TAT6 201
7A278 839
4A7Q5 543
Q33T6 641
JQ99Q 238
A793A 86
78575 187
49T9T 303
8542K 198
25A82 786
77A77 363
244KK 397
87877 689
T6T6T 778
KKKK4 392
22T2J 171
T77A7 174
99KJK 180
2444J 761
QAQQA 508
3373T 121
222TT 137
736T2 8
4444J 12
33555 566
487A4 230
22626 439
QQQQ8 336
5T552 521
QTQQQ 872
63836 614
66695 450
AKAAA 361
95A95 111
644JK 701
ATAAA 840
9J99T 35
3333A 664
99992 99
5A5A5 680
Q7J5J 405
59999 954
2KTQ9 255
6QQ96 149
37333 67
QJ6Q3 425
Q6K29 415
J7277 126
AA29Q 220
AAKJK 831
27QQ2 986
AJ367 891
33292 666
Q8786 933
TAJ28 554
K4448 585
Q8225 931
46242 598
7AT98 693
78J56 369
TTTTJ 371
TKJKK 990
66767 802
56956 789
544K4 275
46699 215
26666 710
AQQQQ 116
6A858 757
55757 142
J2KA4 723
6229K 906
6K366 179
4378K 527
AA664 832
72242 499
2TT2Q 998
5QQQ5 647
5858J 856
555KJ 655
7788J 524
36J5J 82
99666 2
9AKK3 177
47447 596
9266J 572
4TTQ4 946
999J9 110
T74K4 899
Q27J8 156
98AK5 754
3TJ6T 396
75QK7 517
K22KJ 741
9699T 916
66QAQ 202
7329Q 32
89JAT 708
444Q4 841
73366 880
677QK 683
48J84 394
2966T 6
9KT9K 531
8544T 746
2222A 934
T2QKT 615
8J282 430
K9K9K 214
336TJ 904
9JTJ9 777
QKAKK 68
62A66 48
49499 280
97967 46
8T66T 273
94349 379
57777 719
2QT5K 827
4TTT2 40
J2252 859
A3TQA 426
468JA 611
57745 662
424AA 938
J33AT 309
99339 610
872KJ 570
K2395 571
77A5Q 996
JQ7Q6 340
973J9 776
J6K92 14
5J5Q9 191
Q5555 773
A7KA7 11
54545 851
AJ9A8 968
QQAJ7 821
T6JTT 670
3T2TT 151
TTTT3 886
33933 850
KKKQQ 656
T2TT2 54
QA44J 995
89J27 335
3742A 212
T7Q88 260
TT666 75
JKQ28 724
548T3 698
3883K 148
39966 864
69424 320
4QJ5J 332
J4297 346
78A66 605
82333 809
4K777 644
9739T 936
JJ433 755
88688 43
3TJ33 988
J37K3 628
3KK3T 583
A433J 195
89K46 262
JKKQK 545
7AA27 437
977T8 828
757Q7 759
7T7A8 805
33336 19
KJKKK 118
44445 586
84Q8K 205
439K3 909
6J663 762
33777 975
9AAQA 564
JJ8JJ 91
24555 696
62644 707
AA3J5 31
6T2KQ 135
24244 34
3QK3K 560
9J3T9 787
7QTA8 705
A22JA 528
84844 537
87667 937
QJ5K4 90
2K326 239
59878 421
7J377 536
857K7 350
2K2A2 704
39T9T 493
QKT86 829
3AQJT 714
7AAAJ 600
9QK56 983
35K33 193
89888 589
TQ477 432
777K7 59
86AQJ 17
QQQJ3 407
68K25 166
32288 748
TKTQK 65
J9K56 530
AA323 960
27266 112
22722 675
73367 497
Q7JJ3 16
444T4 438
22323 74
3QQ6K 348
A68J8 454
42K64 367
3AA7J 889
AT492 398
AAA6A 673
2Q9QQ 95
A7477 815
4A8KA 444
6JJ66 893
84445 429
7997J 101
A5A6A 958
K27A2 486
4A4J4 327
464TJ 434
5554J 766
KTKT8 801
573K5 64
JKK88 522
A9999 291
T787T 338
39648 502
46464 733
AQAAA 357
8TTTT 797
64444 515
8588T 53
AKAKK 617
8AJ99 888
3TAAJ 874
338Q3 694
TT443 578
86227 419
9J9JQ 736
22522 943
823J3 866
24444 519
A7A7A 207
5JJ99 36
K2896 250
66A5J 129
JJJJJ 668
8J548 721
J8J63 385
3Q442 79
8K868 141
AAA67 767
T968K 289
5QJ5K 245
TKQK5 96
54774 122
89666 568
95596 538
9K3J4 380
66665 20
7759A 209
6JTA6 702
29574 88
AA6A4 181
Q28T4 152
777KQ 386
47888 271
K8KKQ 738
J7337 162
JQA88 820
QQ4QQ 276
TKKTT 728
JTKTK 316
3J334 684
9944Q 830
8485A 753
JKKJ8 221
A9QJ6 948
KJ3KQ 727
Q5QQQ 695
Q8QTQ 542
2QQQ8 119
JJ55Q 845
52725 950
QAJ33 808
JAA7J 557
Q22AA 183
Q8Q8Q 661
J55JT 410
KKQJJ 901
Q5455 539
AQJJ9 956
7QJ7T 896
K5KKJ 994
388KK 822
2QQ2K 218
3J4K3 285
256K7 923
K2KKA 157
QQTKQ 139
48TTT 1
TTT44 770
JJ777 898
Q466K 117
TQ4QQ 172
43333 883
855T3 324
8T933 645
579K9 347
A76Q4 729
3TTT3 115
QJAQA 928
65T65 541
77A7A 625
29J9J 472
TKT74 926
A934K 643
JT533 199
TT4AJ 216
KKKK6 55
6J3K5 676
398J3 18
778K8 653
79773 406
25446 606
54323 76
2T992 601
8A888 241
9AJQK 509
3Q3QK 739
KKKK8 667
JJ494 590
83JKT 203
J9957 751
JJAQQ 296
4835Q 301
333T3 722
KKK2K 535
8Q2QJ 378
5J396 652
TQ7J8 624
5JJ22 447
TT777 62
TTJT7 60
TA2QT 626
T3Q79 791
K6QQ8 294
28K52 967
6K374 663
7877A 495
24226 352
88KAK 907
J242T 546
K77JT 465
Q77Q5 863
TQTQT 284
49K62 26
6TTT8 569
Q77QQ 779
99T6T 70
K4AKA 130
J5TT5 979
KQJT8 175
96K99 648
A56J3 422
53585 58
6QJTQ 794
9QJ77 388
88944 235
55A2Q 780
9Q992 297
96699 660
682QJ 865
6TTTK 799
Q47QQ 358
2929K 920
J5535 752
T3383 631
6737Q 894
9QT72 440
844J9 470
6AA6A 211
5AA5A 981
8KJ88 49
55855 93
5J495 261
8QQQ5 629
J4493 860
ATTKA 818
77264 877
55K58 365
QQ666 106
238T6 459
77QQK 848
5757A 233
84AT3 951
TT7TT 642
72236 345
TQ333 760
77744 720
QQ4QK 478
Q82A2 506
AJ3AA 942
JJ958 526
AJ4A4 341
A26J8 742
5TK28 871
865J3 136
66A6A 556
Q4A55 552
K8429 37
7QQ9Q 442
323J5 375
6J456 608
73Q6A 424
8QQ68 976
J9KA7 286
Q5Q5J 418
74472 154
KKQT9 637
Q8Q88 574
7543A 190
KA87J 456
2877K 635
J76A4 330
83Q69 128
A88A8 377
9999K 630
J698Q 878
67777 243
A232A 844
8K66J 835
QJ36A 420
22QQ6 782
3339K 677
K72QK 563
86777 307
323JJ 972
955KQ 826
33443 259
88Q88 897
85778 939
7T268 356
5T85J 861
833J8 387
4AA4A 7
QTQJJ 725
TT36T 395
6KJAJ 520
5J525 473
T88JT 182
44T66 173
Q75A8 251
K777A 985
88T33 905
99575 949
62222 186
22923 492
J4Q42 706
AA872 500
JA895 153
TT562 295
34434 854
2222J 881
69K46 882
TK74J 274
T46TT 548
K54KJ 164
9999Q 184
3QTJJ 400
8QK59 41
994Q9 1000
347J4 581
Q7289 138"""
    val ins = Day7()
    println(ins.two(inputReal))

}

data class Hand(val hand: List<Card>, val bid: Int) : Comparable<Hand> {
    var counts: Map<Card, Int> = emptyMap()

    fun getRank(): Int {
        return when (counts.values.max()) {
            5 -> 7
            4 -> 6
            3 -> if (counts.values.contains(2)) 5 else 4
            2 -> if (counts.values.count { it == 2 } == 2) 3 else 2
            1 -> 1
            else -> 0
        }
    }

    override fun toString(): String {
        return getRank().toString() + ": " + hand.toString()
    }


    override fun compareTo(other: Hand): Int {
        val left = other.getRank()
        val right = this.getRank()
        return if (left == right) {
            for (i in 0..<other.hand.size) {
                val comp = this.hand[i].value.compareTo(other.hand[i].value)
                if (comp != 0) {
                    return comp
                }
            }
            return 0

        } else {
            compareValues(right, left)
        }
    }

}

enum class Card(val value: Int) {
    _2(1),
    _3(2),
    _4(3),
    _5(4),
    _6(5),
    _7(6),
    _8(7),
    _9(8),
    T(9),
    J(0),
    Q(11),
    K(12),
    A(13);

    companion object {
        fun getCard(c: String) = run {
            try {
                Card.valueOf(c)
            } catch (e: IllegalArgumentException) {
                Card.valueOf("_$c")
            }
        }
    }

    override fun toString(): String {
        return this.name.replace("_", "")
    }
}
package nl.meine.aoc._2023

class Day7 {

    fun one(input: String): Int {
        val lines = createHands(input)

        val h = orderHands(lines)
        h.forEach { println(it) }

        return h.mapIndexed { rank, hand -> (rank + 1) * hand.bid }
            .sum()
    }

    fun createHands(input: String): MutableList<Hand> {
        return input.split("\n")
            .map { it.split(" ") }
            .map { Hand(it[0].chunked(1).map { c -> Card.getCard(c) }.toList(), it[1].toInt()) }
            .map { processJokers(it) }
            .toMutableList()
    }

    fun orderHands(hand: MutableList<Hand>): MutableList<Hand> {
        hand.sort()
        hand.reversed()
        return hand

    }

    fun processJokers(hand: Hand): Hand {
        val initial = hand.hand.filter { it.name != "J" }.groupingBy { it }.eachCount().toMutableMap()
        if (initial.isEmpty()) {
            initial[Card.A] = 0
        }

        val a = initial.maxBy { it.value }.key
        initial[a] = initial[a]!! + hand.hand.count { it.name == "J" }

        hand.counts = initial

        return hand
    }

    fun two(input: String): Int {
        val lines = createHands(input)

        val h = orderHands(lines)
        h.forEach { println(it) }

        return h.mapIndexed { rank, hand -> (rank + 1) * hand.bid }
            .sum()
    }

}


// two: 250665479
  //      250665248
fun main() {
    val inputReal =
        """9A35J 469
75T32 237
6T8JQ 427
3366A 814
K2AK9 982
J8KTT 9
94936 970
Q8AK9 15
3QQ32 940
65555 484
8K88K 674
Q67T5 788
77575 476
KAKAA 785
AA3AA 240
44767 423
Q923A 300
KK444 650
QQQ6Q 313
5JA22 167
7A264 837
TTKTT 646
K62JJ 682
34A63 532
7J554 393
Q86T6 745
9963K 718
92K85 97
3KKJ3 604
98Q26 257
7AJ6Q 132
K48T5 125
554J4 408
T6333 178
5757J 479
8J222 488
KKJ6A 460
95T38 944
23J22 796
AK333 69
25AK9 978
77779 123
K9T83 56
46A39 730
655J5 77
A2T2A 78
J8Q75 847
QA82A 700
TTT67 443
KT9A5 343
6969T 582
A7779 703
3QA93 597
6JT6T 558
28888 290
26TT2 147
T9356 587
6T542 482
3J733 140
TQT72 534
3K3K3 208
5JA6J 833
64646 277
86K68 795
24455 997
3633K 504
K3K36 790
22KK2 731
AA8AA 991
A6666 312
38395 712
46J44 229
52529 268
49AA9 573
J7J94 258
87TKA 947
J2999 555
T9KJK 194
7KK3K 146
T255T 875
9QAT4 475
759A2 169
22322 510
27T69 299
644K6 4
44J84 227
6A722 806
775KJ 25
37425 679
7TA8T 226
3Q6TK 391
8T3J4 196
67534 339
5QQ55 565
J33A3 305
8A8AJ 732
2QQJ5 523
AAAJA 618
97733 858
2A6Q2 433
J8QT3 390
3333J 910
K2AK2 884
AQTTQ 810
55666 855
7Q7QJ 743
K8KK3 489
4AKKJ 619
QQKQQ 373
QJ59A 688
2687J 622
7Q77J 512
578A3 120
T2662 737
JKJK7 807
44T94 223
454Q5 607
75J58 551
A686J 763
TAT4T 108
75356 591
96989 977
K7KTK 775
5252Q 318
77A5A 599
22K87 359
JJQQQ 930
8TKT8 436
A9QQ8 374
4T2JA 30
777AJ 463
8J888 87
42K8Q 657
J5JJ5 713
AKQJ3 45
T59K4 765
22A77 266
K8KK8 219
6T666 349
72T7T 92
3JQJQ 964
AA2J9 672
66997 803
5A8A2 467
KKKQ7 98
67Q5J 669
J57A6 503
98347 331
6J668 279
9K7KK 319
84848 337
33353 974
A333T 576
66636 461
6AAA2 281
Q52QQ 691
7876A 892
533T8 966
5A9Q7 533
A6AAQ 21
688Q6 100
K8483 764
62865 453
39323 189
TTJJT 498
5J437 306
55A7A 876
9A366 562
8J479 249
65544 640
83T88 325
AK45A 321
39QQ3 849
83838 749
43394 602
33722 843
T5KTJ 428
JJKK9 549
33737 27
4KKK4 638
KK398 246
J6828 372
99229 23
2963K 577
3ATJJ 862
6488T 912
48554 903
3A353 627
86923 768
79A98 540
77TTT 965
TK2T7 188
2A644 687
QT943 253
K2QJT 887
J5KK5 915
9Q2JA 980
J237J 28
A46Q2 264
7T2TT 621
A4T8J 57
Q5QT5 217
K9AK7 248
TAJTT 973
82822 919
858AA 265
5555J 800
552J2 908
Q5248 272
A5KK5 529
K8J9T 85
888TT 477
665Q5 823
Q42AT 449
4843A 83
Q5476 885
Q52T2 409
6A2TJ 580
92A99 483
Q9Q6Q 734
36662 134
88899 401
KQQQ9 389
JQ259 658
57895 639
KQ654 852
94273 612
77KT7 987
3Q4AQ 364
6KQK6 651
4AQ23 163
34TTT 107
T98T2 870
J78Q9 756
3K3J3 404
J993J 328
Q8JQA 678
96759 326
2J22J 383
2T222 236
76666 772
QQ5QJ 225
A47AA 505
967TJ 924
Q3TJ4 63
2KK29 879
44Q4Q 685
TTQ9T 659
7244J 414
T22TJ 269
55552 3
J78TK 474
3J3J3 559
8QJ42 382
28T86 740
TKTT8 381
22J44 579
J8668 242
88TT6 709
KKKJ9 192
478K2 501
3Q66J 22
K9KAJ 774
K3333 113
4TJT4 945
Q55KK 609
Q7272 955
6AKAA 490
964T5 971
36Q3Q 51
3T9AT 969
Q8TQT 256
4A833 632
5499J 812
44222 176
6QQ6Q 317
TJT2T 206
A862Q 468
36663 464
54JK9 302
8J828 103
32QQ2 38
66675 595
2J8TQ 50
2QQQQ 783
333QQ 282
67KKK 511
2996J 518
J824K 44
K6656 726
Q7Q5T 793
KQ27T 494
T2KQA 308
AAAA2 441
24J49 716
K58TT 873
7TA49 39
6KQ7T 298
6Q555 455
66AJA 819
5A9A3 922
AAA88 170
42324 962
22Q22 616
44QJQ 399
9J3TA 342
56K3A 588
99T9A 224
56T66 869
77997 671
53222 161
8KKJT 384
37777 567
TKTK5 42
6J622 127
387Q5 417
T3A73 360
28AKA 868
K26Q5 288
J6J88 692
KKKK9 690
2A5QA 435
K83T4 158
QAA3A 104
T7Q66 914
A3A6A 270
J888J 838
3JTKQ 867
7KKKK 451
K7766 824
Q4Q9Q 620
65A33 957
99A9A 781
A9AAA 344
44744 935
6666J 804
A8QJ9 155
AQKAA 681
6K7KJ 853
A99JA 366
65JJ5 47
4TQ8T 769
66555 457
TJ226 222
78777 758
5K488 999
Q65T4 362
4Q738 80
5JJ55 323
8Q65A 918
A5TT5 353
294J3 471
33Q44 102
66668 254
7663Q 516
AAAJJ 798
3344J 10
85888 507
68868 144
588T5 124
TT4TT 834
QQ7QK 168
75555 593
849KA 446
9QT7A 165
5AA7A 697
J99J9 213
6834K 5
TQQQT 292
7A655 403
982AQ 143
A6237 334
QK5T6 963
99895 232
56366 376
34J35 231
AAQAQ 150
78887 485
QQQQJ 263
TAQ98 197
425J5 895
JQ7QQ 992
J9JJ9 61
5888J 665
6J6K6 496
78J82 293
22292 73
TT2TT 911
ATAAT 322
339AA 953
A7465 413
AA4AA 283
QT3TQ 84
QQA5Q 66
J472J 784
T3JTT 131
333A4 210
79299 550
88A8J 594
J9749 584
J735K 902
933JQ 109
5T926 481
4T4T4 561
87TK6 247
98964 159
78997 354
36JT4 29
59464 771
2AA5A 33
77J43 244
37T7T 448
23QJ3 925
K7K5Q 792
QQQQ9 411
KK568 278
49TK7 961
55TAA 817
33J4Q 984
5QJK9 105
9A9J9 623
AK62Q 842
38333 735
9A8J8 333
9T243 204
6K2J3 547
78JKQ 959
AA3A9 634
KJJKT 13
KJKKJ 921
99934 310
4J929 431
55559 445
TT62T 355
32K25 825
622A6 480
334Q9 412
26922 114
638KJ 900
364T8 462
QQJT7 234
TK586 525
K342K 816
TTT99 699
JT658 315
36388 24
2666J 185
53455 592
4AJAA 304
J3QQ8 228
22J2A 416
99768 927
TKJ9A 487
5A55J 744
QKQ44 613
336QA 81
66622 200
JTTTK 717
59ATT 513
33938 813
66AKJ 466
3976Q 941
8888T 491
JJK47 836
99Q7Q 654
Q896K 633
Q8JKQ 553
J6K6K 811
2T323 750
KKAKK 952
Q6JJ4 929
77J77 993
9KQJT 989
44254 857
4AA5A 514
TATJA 715
74422 145
33332 544
5T5QT 311
78JT8 913
JKKT2 94
26QK7 71
444JJ 287
99979 133
Q999Q 314
48888 649
33T8K 368
77T57 711
4T42Q 351
89Q99 89
69666 846
3A33A 890
8Q8A3 636
4A888 603
9TQTA 747
9AAA9 52
TJ4TT 267
J76J7 72
K773Q 370
77Q77 329
636QQ 575
ATQAA 917
2743Q 932
2KKKJ 452
93929 686
4ATTA 458
42222 252
35225 402
45JKA 160
4TAT6 201
7A278 839
4A7Q5 543
Q33T6 641
JQ99Q 238
A793A 86
78575 187
49T9T 303
8542K 198
25A82 786
77A77 363
244KK 397
87877 689
T6T6T 778
KKKK4 392
22T2J 171
T77A7 174
99KJK 180
2444J 761
QAQQA 508
3373T 121
222TT 137
736T2 8
4444J 12
33555 566
487A4 230
22626 439
QQQQ8 336
5T552 521
QTQQQ 872
63836 614
66695 450
AKAAA 361
95A95 111
644JK 701
ATAAA 840
9J99T 35
3333A 664
99992 99
5A5A5 680
Q7J5J 405
59999 954
2KTQ9 255
6QQ96 149
37333 67
QJ6Q3 425
Q6K29 415
J7277 126
AA29Q 220
AAKJK 831
27QQ2 986
AJ367 891
33292 666
Q8786 933
TAJ28 554
K4448 585
Q8225 931
46242 598
7AT98 693
78J56 369
TTTTJ 371
TKJKK 990
66767 802
56956 789
544K4 275
46699 215
26666 710
AQQQQ 116
6A858 757
55757 142
J2KA4 723
6229K 906
6K366 179
4378K 527
AA664 832
72242 499
2TT2Q 998
5QQQ5 647
5858J 856
555KJ 655
7788J 524
36J5J 82
99666 2
9AKK3 177
47447 596
9266J 572
4TTQ4 946
999J9 110
T74K4 899
Q27J8 156
98AK5 754
3TJ6T 396
75QK7 517
K22KJ 741
9699T 916
66QAQ 202
7329Q 32
89JAT 708
444Q4 841
73366 880
677QK 683
48J84 394
2966T 6
9KT9K 531
8544T 746
2222A 934
T2QKT 615
8J282 430
K9K9K 214
336TJ 904
9JTJ9 777
QKAKK 68
62A66 48
49499 280
97967 46
8T66T 273
94349 379
57777 719
2QT5K 827
4TTT2 40
J2252 859
A3TQA 426
468JA 611
57745 662
424AA 938
J33AT 309
99339 610
872KJ 570
K2395 571
77A5Q 996
JQ7Q6 340
973J9 776
J6K92 14
5J5Q9 191
Q5555 773
A7KA7 11
54545 851
AJ9A8 968
QQAJ7 821
T6JTT 670
3T2TT 151
TTTT3 886
33933 850
KKKQQ 656
T2TT2 54
QA44J 995
89J27 335
3742A 212
T7Q88 260
TT666 75
JKQ28 724
548T3 698
3883K 148
39966 864
69424 320
4QJ5J 332
J4297 346
78A66 605
82333 809
4K777 644
9739T 936
JJ433 755
88688 43
3TJ33 988
J37K3 628
3KK3T 583
A433J 195
89K46 262
JKKQK 545
7AA27 437
977T8 828
757Q7 759
7T7A8 805
33336 19
KJKKK 118
44445 586
84Q8K 205
439K3 909
6J663 762
33777 975
9AAQA 564
JJ8JJ 91
24555 696
62644 707
AA3J5 31
6T2KQ 135
24244 34
3QK3K 560
9J3T9 787
7QTA8 705
A22JA 528
84844 537
87667 937
QJ5K4 90
2K326 239
59878 421
7J377 536
857K7 350
2K2A2 704
39T9T 493
QKT86 829
3AQJT 714
7AAAJ 600
9QK56 983
35K33 193
89888 589
TQ477 432
777K7 59
86AQJ 17
QQQJ3 407
68K25 166
32288 748
TKTQK 65
J9K56 530
AA323 960
27266 112
22722 675
73367 497
Q7JJ3 16
444T4 438
22323 74
3QQ6K 348
A68J8 454
42K64 367
3AA7J 889
AT492 398
AAA6A 673
2Q9QQ 95
A7477 815
4A8KA 444
6JJ66 893
84445 429
7997J 101
A5A6A 958
K27A2 486
4A4J4 327
464TJ 434
5554J 766
KTKT8 801
573K5 64
JKK88 522
A9999 291
T787T 338
39648 502
46464 733
AQAAA 357
8TTTT 797
64444 515
8588T 53
AKAKK 617
8AJ99 888
3TAAJ 874
338Q3 694
TT443 578
86227 419
9J9JQ 736
22522 943
823J3 866
24444 519
A7A7A 207
5JJ99 36
K2896 250
66A5J 129
JJJJJ 668
8J548 721
J8J63 385
3Q442 79
8K868 141
AAA67 767
T968K 289
5QJ5K 245
TKQK5 96
54774 122
89666 568
95596 538
9K3J4 380
66665 20
7759A 209
6JTA6 702
29574 88
AA6A4 181
Q28T4 152
777KQ 386
47888 271
K8KKQ 738
J7337 162
JQA88 820
QQ4QQ 276
TKKTT 728
JTKTK 316
3J334 684
9944Q 830
8485A 753
JKKJ8 221
A9QJ6 948
KJ3KQ 727
Q5QQQ 695
Q8QTQ 542
2QQQ8 119
JJ55Q 845
52725 950
QAJ33 808
JAA7J 557
Q22AA 183
Q8Q8Q 661
J55JT 410
KKQJJ 901
Q5455 539
AQJJ9 956
7QJ7T 896
K5KKJ 994
388KK 822
2QQ2K 218
3J4K3 285
256K7 923
K2KKA 157
QQTKQ 139
48TTT 1
TTT44 770
JJ777 898
Q466K 117
TQ4QQ 172
43333 883
855T3 324
8T933 645
579K9 347
A76Q4 729
3TTT3 115
QJAQA 928
65T65 541
77A7A 625
29J9J 472
TKT74 926
A934K 643
JT533 199
TT4AJ 216
KKKK6 55
6J3K5 676
398J3 18
778K8 653
79773 406
25446 606
54323 76
2T992 601
8A888 241
9AJQK 509
3Q3QK 739
KKKK8 667
JJ494 590
83JKT 203
J9957 751
JJAQQ 296
4835Q 301
333T3 722
KKK2K 535
8Q2QJ 378
5J396 652
TQ7J8 624
5JJ22 447
TT777 62
TTJT7 60
TA2QT 626
T3Q79 791
K6QQ8 294
28K52 967
6K374 663
7877A 495
24226 352
88KAK 907
J242T 546
K77JT 465
Q77Q5 863
TQTQT 284
49K62 26
6TTT8 569
Q77QQ 779
99T6T 70
K4AKA 130
J5TT5 979
KQJT8 175
96K99 648
A56J3 422
53585 58
6QJTQ 794
9QJ77 388
88944 235
55A2Q 780
9Q992 297
96699 660
682QJ 865
6TTTK 799
Q47QQ 358
2929K 920
J5535 752
T3383 631
6737Q 894
9QT72 440
844J9 470
6AA6A 211
5AA5A 981
8KJ88 49
55855 93
5J495 261
8QQQ5 629
J4493 860
ATTKA 818
77264 877
55K58 365
QQ666 106
238T6 459
77QQK 848
5757A 233
84AT3 951
TT7TT 642
72236 345
TQ333 760
77744 720
QQ4QK 478
Q82A2 506
AJ3AA 942
JJ958 526
AJ4A4 341
A26J8 742
5TK28 871
865J3 136
66A6A 556
Q4A55 552
K8429 37
7QQ9Q 442
323J5 375
6J456 608
73Q6A 424
8QQ68 976
J9KA7 286
Q5Q5J 418
74472 154
KKQT9 637
Q8Q88 574
7543A 190
KA87J 456
2877K 635
J76A4 330
83Q69 128
A88A8 377
9999K 630
J698Q 878
67777 243
A232A 844
8K66J 835
QJ36A 420
22QQ6 782
3339K 677
K72QK 563
86777 307
323JJ 972
955KQ 826
33443 259
88Q88 897
85778 939
7T268 356
5T85J 861
833J8 387
4AA4A 7
QTQJJ 725
TT36T 395
6KJAJ 520
5J525 473
T88JT 182
44T66 173
Q75A8 251
K777A 985
88T33 905
99575 949
62222 186
22923 492
J4Q42 706
AA872 500
JA895 153
TT562 295
34434 854
2222J 881
69K46 882
TK74J 274
T46TT 548
K54KJ 164
9999Q 184
3QTJJ 400
8QK59 41
994Q9 1000
347J4 581
Q7289 138"""
    val ins = Day7()
    println(ins.two(inputReal))

}

data class Hand(val hand: List<Card>, val bid: Int) : Comparable<Hand> {
    var counts: Map<Card, Int> = emptyMap()

    fun getRank(): Int {
        return when (counts.values.max()) {
            5 -> 7
            4 -> 6
            3 -> if (counts.values.contains(2)) 5 else 4
            2 -> if (counts.values.count { it == 2 } == 2) 3 else 2
            1 -> 1
            else -> 0
        }
    }

    override fun toString(): String {
        return getRank().toString() + ": " + hand.toString()
    }


    override fun compareTo(other: Hand): Int {
        val left = other.getRank()
        val right = this.getRank()
        return if (left == right) {
            for (i in 0..<other.hand.size) {
                val comp = this.hand[i].value.compareTo(other.hand[i].value)
                if (comp != 0) {
                    return comp
                }
            }
            return 0

        } else {
            compareValues(right, left)
        }
    }

}

enum class Card(val value: Int) {
    _2(1),
    _3(2),
    _4(3),
    _5(4),
    _6(5),
    _7(6),
    _8(7),
    _9(8),
    T(9),
    J(0),
    Q(11),
    K(12),
    A(13);

    companion object {
        fun getCard(c: String) = run {
            try {
                Card.valueOf(c)
            } catch (e: IllegalArgumentException) {
                Card.valueOf("_$c")
            }
        }
    }

    override fun toString(): String {
        return this.name.replace("_", "")
    }
}