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