aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2022/day-02/day-02.ipynb74
1 files changed, 37 insertions, 37 deletions
diff --git a/2022/day-02/day-02.ipynb b/2022/day-02/day-02.ipynb
index ed080ea..c308201 100644
--- a/2022/day-02/day-02.ipynb
+++ b/2022/day-02/day-02.ipynb
@@ -11,12 +11,14 @@
"\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?"
+ "**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": 45,
+ "execution_count": 7,
"metadata": {
"vscode": {
"languageId": "racket"
@@ -24,21 +26,20 @@
},
"outputs": [],
"source": [
- "(require racket\n",
- " advent-of-code\n",
- " threading)"
+ "#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`, `C` and the second column is `X`, `Y` or `Z`."
+ "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": 46,
+ "execution_count": 8,
"metadata": {
"vscode": {
"languageId": "racket"
@@ -46,20 +47,20 @@
},
"outputs": [],
"source": [
- "(define strategy-guide (~> (fetch-aoc-input (find-session) 2022 2) (string-split \"\\n\")))"
+ "(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": [
- "##### 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."
+ "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": 47,
+ "execution_count": 9,
"metadata": {
"vscode": {
"languageId": "racket"
@@ -67,15 +68,15 @@
},
"outputs": [],
"source": [
- "(define score-bonus (hash 'rock 1 'paper 2 'scissors 3 'win 6 'draw 3 'lose 0))\n",
+ "(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",
+ "(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",
+ " [(cons x y) #:when (eq? y (winning-rounds x)) 'win]\n",
" [_ 'lose]))\n"
]
},
@@ -83,12 +84,14 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "In part 1, we assume that the second column refers to the throw we should select in each round."
+ "##### 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": 48,
+ "execution_count": 10,
"metadata": {
"vscode": {
"languageId": "racket"
@@ -104,19 +107,18 @@
"13809"
]
},
- "execution_count": 48,
+ "execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
- "(define assume-throw (hash \"A\" 'rock \"B\" 'paper \"C\" 'scissors \"X\" 'rock \"Y\" 'paper \"Z\" 'scissors))\n",
+ "(define assume-throw (dict-merge opponent-throw {\"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"
+ "(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))))\n"
]
},
{
@@ -124,12 +126,12 @@
"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."
+ "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": 49,
+ "execution_count": 11,
"metadata": {
"vscode": {
"languageId": "racket"
@@ -145,25 +147,23 @@
"12316"
]
},
- "execution_count": 49,
+ "execution_count": 11,
"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 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) (dict-ref winning-rounds x)]\n",
- "[(x 'lose) (dict-ref losing-rounds x)]))\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 (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"
+ " (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)))))\n"
]
}
],