-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07.rhm
187 lines (157 loc) · 4.15 KB
/
07.rhm
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#lang rhombus/static/and_meta
import:
"util/advent_of_code.rhm" as aoc
"util/misc.rhm" as util
"util/parsec.rhm" as p
@util.example_input(test_input){
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483@("\n")
}
annot.macro 'CompareResult':
'matching(#'lesser || #'equal || #'greater)'
interface Comparable:
method compare(other) :: CompareResult
method less(other) :: Boolean:
compare(other) == #'lesser
fun compare :: CompareResult
| compare(a :: Number, b :: Number):
cond
| a < b: #'lesser
| a .= b: #'equal
| a > b: #'greater
| compare(a :: Comparable, b :: Comparable):
a.compare(b)
namespace C:
export compare
expr.macro
| 'refine_compare
| $compare_expr':
'$compare_expr'
| 'refine_compare
| $compare_expr
| $compare_rest
| ...':
'block:
def res: $compare_expr
if res == #'equal
| refine_compare
| $compare_rest
| ...
| res'
expr.macro 'compare_enum ($a, $b):
$(vals :: Block)':
let [_, &vals] = vals.unwrap()
def [vals_n, ...] = List.iota(vals.length())
let [vals, ...] = vals
'block:
fun to_val(v):
match v
| $vals: $vals_n
| ...
compare(to_val($a), to_val($b))'
expr.macro 'compare_case $compare_expr
| $(lesser_case)
| $(equal_case)
| $(greater_case)':
'match $compare_expr
| #'lesser: $lesser_case
| #'equal: $equal_case
| #'greater: $greater_case'
class Hand(cards :: List.of(PosInt),
bid :: PosInt,
hand_type :: List.of(PosInt)):
implements Comparable
constructor (cards :: List.of(PosInt), bid :: PosInt):
def counts :~ Map:
for values(counts = {1: 0}):
each c: cards
counts ++ { c: counts.get(c, 0) + 1 }
let Map{1: num_jokers, &counts}: counts
def hand_type:
match counts.values().sort(math.greater)
| []: [num_jokers]
| [a, &rest]: List.cons(a + num_jokers, rest)
super(cards, bid, hand_type)
property hand_type_name :: Symbol:
match hand_type
| [5]: #'five
| [4, 1]: #'four
| [3, 2]: #'full_house
| [3, 1, 1]: #'three
| [2, 2, 1]: #'two_pair
| [2, 1, 1, 1]: #'pair
| [1, 1, 1, 1, 1]: #'high
method compare_hand_type(other :: Hand) :: CompareResult:
compare_enum (hand_type_name, other.hand_type_name):
#'high
#'pair
#'two_pair
#'three
#'full_house
#'four
#'five
method compare_cards(a :: List, b :: List) :: CompareResult:
cond
| a == [] && b == []: #'equal
| ~else:
refine_compare
| C.compare(a.first, b.first)
| compare_cards(a.rest, b.rest)
override method compare(other :: Hand) :: CompareResult:
refine_compare
| compare_hand_type(other)
| compare_cards(cards, other.cards)
operator a_p <|> b_p:
p.choice(a_p, b_p)
fun char_val_p(str :: String, val):
p.try(p.parse_sequence:
p.char(str[0])
p.pure(val))
fun card_p(j_value):
char_val_p("2", 2) <|> char_val_p("3", 3) <|> char_val_p("4", 4)
<|> char_val_p("5", 5) <|> char_val_p("6", 6) <|> char_val_p("7", 7)
<|> char_val_p("8", 8) <|> char_val_p("9", 9) <|> char_val_p("T", 10)
<|> char_val_p("J", j_value) <|> char_val_p("Q", 12) <|> char_val_p("K", 13)
<|> char_val_p("A", 14)
fun hand_p(j_value):
let card_p: card_p(j_value)
p.parse_sequence:
a = card_p
b = card_p
c = card_p
d = card_p
e = card_p
p.pure([a,b,c,d,e])
fun line_p(j_value):
p.parse_sequence:
hand = hand_p(j_value)
p.spaces
bid = p.integer
p.char(#{#\newline})
p.pure(Hand(hand, bid))
fun input_p(j_value):
p.many1(line_p(j_value))
fun make_run(j_value):
fun (input):
def values(hands :: List.of(Hand), []):
p.parse_string(input_p(j_value), input)
let hands :~ List.of(Hand):
hands.sort(Hand.less)
for values(sum = 0):
each:
h: hands
i: 1..
sum + i * h.bid
def run1 = make_run(11)
check:
run1(test_input) ~is 6440
module part1:
run1(aoc.fetch_input(aoc.find_session(), 2023, 7))
def run2 = make_run(1)
check:
run2(test_input) ~is 5905
module part2:
run2(aoc.fetch_input(aoc.find_session(), 2023, 7))