This repository has been archived by the owner on Nov 22, 2024. It is now read-only.
generated from mazharenko/aoc-agent-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.fs
128 lines (116 loc) · 4.77 KB
/
day07.fs
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
121
122
123
124
125
126
127
128
module impl.day07
#nowarn "25"
open System
module Part1 =
[<CustomComparison>]
[<CustomEquality>]
type Hand =
| Cards of string
override this.Equals(other) =
match other with
| :? Hand as (Cards otherCards) ->
let (Cards cards) = this
cards = otherCards
| _ -> false
override this.GetHashCode() =
let (Cards cards) = this
cards.GetHashCode();
interface IComparable with
member this.CompareTo other =
(this :> IComparable<Hand>).CompareTo (other :?> Hand)
interface IComparable<Hand> with
member this.CompareTo (Cards otherCards) =
let (Cards cards) = this
let handRank cards =
let counts = cards |> Seq.group id Seq.length |> Seq.map snd |> Seq.sortDescending |> List.ofSeq
match counts with
| 5 :: _ -> 7
| 4 :: _ -> 6
| 3 :: 2 :: _ -> 5
| 3 :: _ -> 4
| 2 :: 2 :: _ -> 3
| 2 :: _ -> 2
| _ -> 1
let handComparison = compare (handRank otherCards) (handRank cards)
if (handComparison = 0)
then
let allCardsFromWeakestToStrongest = "23456789TJQKA"
// build array of card strengths - indexes in the string above
// arrays are compared properly in F#
let cardStrengthArray =
Seq.toArray >> Array.map (string >> String.indexOf allCardsFromWeakestToStrongest)
compare
(otherCards |> cardStrengthArray)
(cards |> cardStrengthArray)
else handComparison
let parse input =
Pattern1.read (fun line ->
let hand, bet = splitToTuple2 [| " " |] line
Cards hand, int bet
) input
let solve input =
input
|> Array.sortByDescending fst
|> Array.mapi (fun i x -> (i + 1) * (snd x))
|> Array.sum
module Part2 =
[<CustomComparison>]
[<CustomEquality>]
type Hand =
| Cards of string
override this.Equals(other) =
match other with
| :? Hand as (Cards otherCards) ->
let (Cards cards) = this
cards = otherCards
| _ -> false
override this.GetHashCode() =
let (Cards cards) = this
cards.GetHashCode();
interface IComparable with
member this.CompareTo other =
(this :> IComparable<Hand>).CompareTo (other :?> Hand)
interface IComparable<Hand> with
member this.CompareTo (Cards otherCards) =
let (Cards cards) = this
let handRank cards =
let jokerCounts, restCounts =
cards
|> Seq.countBy id
|> Seq.sortByDescending snd |> List.ofSeq |> List.partition (fst >> ((=)'J'))
let countsWithJokers =
List.map snd <|
match (jokerCounts, restCounts) with
| [], _ -> restCounts
| jokers, [] -> jokers
| [(_, jokers)], (first, firstCount)::rest -> (first, firstCount + jokers)::rest
match countsWithJokers with
| 5 :: _ -> 7
| 4 :: _ -> 6
| 3 :: 2 :: _ -> 5
| 3 :: _ -> 4
| 2 :: 2 :: _ -> 3
| 2 :: _ -> 2
| _ -> 1
let handComparison = compare (handRank otherCards) (handRank cards)
if (handComparison = 0)
then
let allCardsFromWeakestToStrongest = "J23456789TQKA"
// build array of card strengths - indexes in the string above
// arrays are compared properly in F#
let cardStrengthArray =
Seq.toArray >> Array.map (string >> String.indexOf allCardsFromWeakestToStrongest)
compare
(otherCards |> cardStrengthArray)
(cards |> cardStrengthArray)
else handComparison
let parse input =
Pattern1.read (fun line ->
let hand, bet = splitToTuple2 [| " " |] line
Cards hand, int bet
) input
let solve input =
input
|> Array.sortByDescending fst
|> Array.mapi (fun i x -> (i + 1) * (snd x))
|> Array.sum