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?\n",
"\n",
"For this solution, I'm using `rackjure` since I'm planning on using a bunch of dictionaries, and `rackjure`'s shorthand makes them easier to work with."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"vscode": {
"languageId": "racket"
}
},
"outputs": [],
"source": [
"#lang iracket/lang #:require rackjure\n",
"(require advent-of-code)"
]
},
{
"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` or `C` (corresponding to the opponent's throw of rock, paper or scissors) and the second column is `X`, `Y` or `Z`, whose meaning is currently unknown."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"vscode": {
"languageId": "racket"
}
},
"outputs": [],
"source": [
"(define strategy-guide (~> (fetch-aoc-input (find-session) 2022 2) (string-split \"\\n\")))\n",
"(define opponent-throw {\"A\" 'rock \"B\" 'paper \"C\" 'scissors})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We're also given the score 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": 3,
"metadata": {
"vscode": {
"languageId": "racket"
}
},
"outputs": [],
"source": [
"(define score-bonus {'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* (them me)\n",
" [(x x) 'draw]\n",
" [(x y) #:when (eq? y (winning-rounds x)) 'win]\n",
" [(_ _) 'lose]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Part 1\n",
"\n",
"In part 1, we assume that the second column refers to the throw we should select in each round. We add that to our existing dictionary and write a `for` comprehension to calculate each round result."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"vscode": {
"languageId": "racket"
}
},
"outputs": [
{
"data": {
"text/html": [
"<code>13809</code>"
],
"text/plain": [
"13809"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(define assume-throw (dict-merge opponent-throw {\"X\" 'rock \"Y\" 'paper \"Z\" 'scissors}))\n",
"\n",
"(for/sum ([play (in-list strategy-guide)])\n",
" (match-define (list them me) (string-split play))\n",
" (+ (score-bonus (outcome (assume-throw them) (assume-throw me)))\n",
" (score-bonus (assume-throw me))))"
]
},
{
"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 look up what we should throw in response for each round, and then calculate the score from that."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"vscode": {
"languageId": "racket"
}
},
"outputs": [
{
"data": {
"text/html": [
"<code>12316</code>"
],
"text/plain": [
"12316"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(define assume-result (dict-merge opponent-throw {\"X\" 'lose \"Y\" 'draw \"Z\" 'win}))\n",
"(define (pick-throw them result)\n",
" (match* (them result)\n",
" [(x 'draw) x]\n",
" [(x 'win) (winning-rounds x)]\n",
" [(x 'lose) (losing-rounds x)]))\n",
"\n",
"(for/sum ([play (in-list strategy-guide)])\n",
" (match-define (list them result) (string-split play))\n",
" (+ (score-bonus (assume-result result))\n",
" (score-bonus (pick-throw (assume-result them) (assume-result result)))))"
]
}
],
"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
}
|