{
"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": [
"13809
"
],
"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": [
"12316
"
],
"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
}