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
188
189
190
191
192
193
|
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Advent of Code 2022\n",
"#### Day 2: Rock Paper Scissors\n",
"\n",
"You've given a strategy guide for how to win at a Rock Paper Scissors tournament. The first column is what your opponent will throw. Your score is determined by the result (win, lose, draw) of each round and what you played (rock, paper, scissors).\n",
"\n",
"**Part 1.** What's your tournament score if the second column represents what you should play in each round?\n",
"\n",
"**Part 2.** What's your tournament score if the second column represents the result of each round?"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"vscode": {
"languageId": "racket"
}
},
"outputs": [],
"source": [
"(require racket\n",
" advent-of-code\n",
" threading)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The input for this problem is a list with two columns; the first column is one of the characters `A`, `B`, `C` and the second column is `X`, `Y` or `Z`."
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"vscode": {
"languageId": "racket"
}
},
"outputs": [],
"source": [
"(define strategy-guide (~> (fetch-aoc-input (find-session) 2022 2) (string-split \"\\n\")))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Part 1\n",
"We're given the bonus for a round result and the bonus for the selected throw, and we write a function that determines the result for a given round."
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"vscode": {
"languageId": "racket"
}
},
"outputs": [],
"source": [
"(define score-bonus (hash 'rock 1 'paper 2 'scissors 3 'win 6 'draw 3 'lose 0))\n",
"\n",
"(define winning-rounds '((rock . paper) (paper . scissors) (scissors . rock)))\n",
"(define losing-rounds '((rock . scissors) (paper . rock) (scissors . paper)))\n",
"\n",
"(define (outcome them me)\n",
" (match (cons them me)\n",
" [(cons x x) 'draw]\n",
" [winner #:when (member winner winning-rounds) 'win]\n",
" [_ 'lose]))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In part 1, we assume that the second column refers to the throw we should select in each round."
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"vscode": {
"languageId": "racket"
}
},
"outputs": [
{
"data": {
"text/html": [
"<code>13809</code>"
],
"text/plain": [
"13809"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(define assume-throw (hash \"A\" 'rock \"B\" 'paper \"C\" 'scissors \"X\" 'rock \"Y\" 'paper \"Z\" 'scissors))\n",
"\n",
"(for/sum\n",
" ([play (in-list strategy-guide)])\n",
" (match-define (list (app (curry hash-ref assume-throw) them) (app (curry hash-ref assume-throw) me))\n",
" (string-split play))\n",
" (+ (hash-ref score-bonus (outcome them me)) (hash-ref score-bonus me)))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Part 2\n",
"Now we're told that the second column actually represents the round result: `X` is lose, `Y` is draw, `Z` is win. We can use pattern matching to look up what we should throw in response for each round, and then calculate the score from that."
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"vscode": {
"languageId": "racket"
}
},
"outputs": [
{
"data": {
"text/html": [
"<code>12316</code>"
],
"text/plain": [
"12316"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(define assume-result (hash \"A\" 'rock \"B\" 'paper \"C\" 'scissors \"X\" 'lose \"Y\" 'draw \"Z\" 'win))\n",
"\n",
"(define (pick-throw them result)\n",
"(match* (them result)\n",
"[(x 'draw) x]\n",
"[(x 'win) (dict-ref winning-rounds x)]\n",
"[(x 'lose) (dict-ref losing-rounds x)]))\n",
"\n",
"(for/sum ([play (in-list strategy-guide)])\n",
" (match-define (list (app (curry hash-ref assume-result) them)\n",
" (app (curry hash-ref assume-result) result))\n",
" (string-split play))\n",
" (+ (hash-ref score-bonus result) (hash-ref score-bonus (pick-throw them result))))\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Racket (trusted)",
"language": "racket",
"name": "racket-trusted"
},
"language_info": {
"codemirror_mode": "scheme",
"file_extension": ".rkt",
"mimetype": "text/x-racket",
"name": "Racket",
"pygments_lexer": "racket",
"version": "8.7"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|