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
|
{
"cells": [
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"(require racket\n",
" advent-of-code\n",
" threading)"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"(define strategy-guide-raw (~> (fetch-aoc-input (find-session) 2022 2) (string-split \"\\n\")))\n",
"\n",
"(define translation-part-1\n",
" (hash \"A\" 'rock \"B\" 'paper \"C\" 'scissors \"X\" 'rock \"Y\" 'paper \"Z\" 'scissors))\n",
"\n",
"(define score-bonus (hash 'rock 1 'paper 2 'scissors 3 'win 6 'draw 3 'lose 0))\n",
"\n",
"(define (naive-play them me)\n",
" (match* ((hash-ref translation-part-1 them) (hash-ref translation-part-1 me))\n",
" [(x x) (+ 3 (hash-ref score-bonus x))]\n",
" [('rock (and x 'paper)) (+ 6 (hash-ref score-bonus x))]\n",
" [('paper (and x 'scissors)) (+ 6 (hash-ref score-bonus x))]\n",
" [('scissors (and x 'rock)) (+ 6 (hash-ref score-bonus x))]\n",
" [(_ x) (hash-ref score-bonus x)]))\n"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>13809</code>"
],
"text/plain": [
"13809"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"(define trust-the-guide\n",
" (for/sum ([play (in-list strategy-guide-raw)])\n",
" (match-define (list them me) (string-split play))\n",
" (naive-play them me)))\n",
"\n",
"trust-the-guide\n"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<code>12316</code>"
],
"text/plain": [
"12316"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(define translation-part-2 (hash \"A\" 'rock \"B\" 'paper \"C\" 'scissors \"X\" 'lose \"Y\" 'draw \"Z\" 'win))\n",
"\n",
"(define (skilled-play them me)\n",
" (match* ((hash-ref translation-part-2 them) (hash-ref translation-part-2 me))\n",
" [('rock 'win) 8]\n",
" [('rock 'draw) 4]\n",
" [('rock 'lose) 3]\n",
" [('paper 'win) 9]\n",
" [('paper 'draw) 5]\n",
" [('paper 'lose) 1]\n",
" [('scissors 'win) 7]\n",
" [('scissors 'draw) 6]\n",
" [('scissors 'lose) 2]))\n",
"\n",
"(define new-understanding\n",
" (for/sum ([play (in-list strategy-guide-raw)])\n",
" (match-define (list them me) (string-split play))\n",
" (skilled-play them me)))\n",
"\n",
"new-understanding\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
}
|