aboutsummaryrefslogtreecommitdiff
path: root/2022/day-02
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-12-02 11:02:43 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-12-02 11:02:43 -0500
commit974210a333247e279236eb0d7eb5b5f8c5e8d35c (patch)
tree4c844bf4426bb3f0f98bd230e3208cb5c4922160 /2022/day-02
parent602523b187959efbd1b3b5fde4a8d94ee9407172 (diff)
downloadgleam_aoc-974210a333247e279236eb0d7eb5b5f8c5e8d35c.tar.gz
gleam_aoc-974210a333247e279236eb0d7eb5b5f8c5e8d35c.zip
day 2 revisions
Diffstat (limited to '2022/day-02')
-rw-r--r--2022/day-02/day-02.ipynb156
1 files changed, 109 insertions, 47 deletions
diff --git a/2022/day-02/day-02.ipynb b/2022/day-02/day-02.ipynb
index 1041425..ed080ea 100644
--- a/2022/day-02/day-02.ipynb
+++ b/2022/day-02/day-02.ipynb
@@ -1,9 +1,27 @@
{
"cells": [
{
- "cell_type": "code",
- "execution_count": 56,
+ "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",
@@ -12,31 +30,70 @@
]
},
{
+ "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": 57,
+ "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 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"
+ "(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": "code",
- "execution_count": 58,
+ "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": {
@@ -47,25 +104,37 @@
"13809"
]
},
- "execution_count": 58,
+ "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",
- "(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"
+ "(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": "code",
- "execution_count": 59,
+ "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": {
@@ -76,38 +145,31 @@
"12316"
]
},
- "execution_count": 59,
+ "execution_count": 49,
"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",
+ "(define assume-result (hash \"A\" 'rock \"B\" 'paper \"C\" 'scissors \"X\" 'lose \"Y\" 'draw \"Z\" 'win))\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",
+ "(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",
- "new-understanding\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)",
+ "display_name": "Racket (trusted)",
"language": "racket",
"name": "racket-trusted"
},
@@ -115,7 +177,7 @@
"codemirror_mode": "scheme",
"file_extension": ".rkt",
"mimetype": "text/x-racket",
- "name": "racket",
+ "name": "Racket",
"pygments_lexer": "racket",
"version": "8.7"
},