aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-12-02 23:13:46 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-12-02 23:13:46 -0500
commitf5c99dbecabad67504aa9b914d1ab002208862fd (patch)
treef62421e750a9a0e2442b9386de8bc0fbabfc1745
parenta971dcfd158c8edb2b74c5ac8a32cf47ece26928 (diff)
parent3d979e5bcc7808d231f4749e810382abfb57a831 (diff)
downloadgleam_aoc-f5c99dbecabad67504aa9b914d1ab002208862fd.tar.gz
gleam_aoc-f5c99dbecabad67504aa9b914d1ab002208862fd.zip
Merge branch 'main' of https://github.com/hunkyjimpjorps/AdventOfCode into main
-rw-r--r--2022/day-01/day-01.ipynb24
-rw-r--r--2022/day-02/day-02.ipynb162
-rw-r--r--2022/day-02/day-02.pl67
3 files changed, 185 insertions, 68 deletions
diff --git a/2022/day-01/day-01.ipynb b/2022/day-01/day-01.ipynb
index d65785e..c79a3f6 100644
--- a/2022/day-01/day-01.ipynb
+++ b/2022/day-01/day-01.ipynb
@@ -17,11 +17,7 @@
{
"cell_type": "code",
"execution_count": 1,
- "metadata": {
- "vscode": {
- "languageId": "racket"
- }
- },
+ "metadata": {},
"outputs": [],
"source": [
"(require racket\n",
@@ -50,11 +46,7 @@
{
"cell_type": "code",
"execution_count": 2,
- "metadata": {
- "vscode": {
- "languageId": "racket"
- }
- },
+ "metadata": {},
"outputs": [
{
"data": {
@@ -100,11 +92,7 @@
{
"cell_type": "code",
"execution_count": 3,
- "metadata": {
- "vscode": {
- "languageId": "racket"
- }
- },
+ "metadata": {},
"outputs": [
{
"data": {
@@ -132,15 +120,15 @@
],
"metadata": {
"kernelspec": {
- "display_name": "Racket (WSL)",
+ "display_name": "Racket (trusted)",
"language": "racket",
- "name": "racket"
+ "name": "racket-trusted"
},
"language_info": {
"codemirror_mode": "scheme",
"file_extension": ".rkt",
"mimetype": "text/x-racket",
- "name": "Racket",
+ "name": "racket",
"pygments_lexer": "racket",
"version": "8.7"
},
diff --git a/2022/day-02/day-02.ipynb b/2022/day-02/day-02.ipynb
index 1041425..13b9986 100644
--- a/2022/day-02/day-02.ipynb
+++ b/2022/day-02/day-02.ipynb
@@ -1,42 +1,102 @@
{
"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?\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": [
- "(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` 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": 57,
+ "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 strategy-guide-raw (~> (fetch-aoc-input (find-session) 2022 2) (string-split \"\\n\")))\n",
+ "(define score-bonus {'rock 1 'paper 2 'scissors 3 'win 6 'draw 3 'lose 0})\n",
"\n",
- "(define translation-part-1\n",
- " (hash \"A\" 'rock \"B\" 'paper \"C\" 'scissors \"X\" 'rock \"Y\" 'paper \"Z\" 'scissors))\n",
+ "(define winning-rounds {'rock 'paper 'paper 'scissors 'scissors 'rock})\n",
+ "(define losing-rounds {'rock 'scissors 'paper 'rock 'scissors 'paper})\n",
"\n",
- "(define score-bonus (hash 'rock 1 'paper 2 'scissors 3 'win 6 'draw 3 'lose 0))\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",
- "(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"
+ "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": 58,
- "metadata": {},
+ "execution_count": 4,
+ "metadata": {
+ "vscode": {
+ "languageId": "racket"
+ }
+ },
"outputs": [
{
"data": {
@@ -47,25 +107,36 @@
"13809"
]
},
- "execution_count": 58,
+ "execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
+ "(define assume-throw (dict-merge opponent-throw {\"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 ([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": "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 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": {
@@ -76,38 +147,29 @@
"12316"
]
},
- "execution_count": 59,
+ "execution_count": 5,
"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",
+ "(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",
- "new-understanding\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)",
+ "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"
},
diff --git a/2022/day-02/day-02.pl b/2022/day-02/day-02.pl
new file mode 100644
index 0000000..6561ea5
--- /dev/null
+++ b/2022/day-02/day-02.pl
@@ -0,0 +1,67 @@
+:- use_module(library(yall)).
+:- use_module(library(apply)).
+
+% Facts
+
+game(X, X, draw).
+game(rock, scissors, win).
+game(scissors, paper, win).
+game(paper, rock, win).
+game(rock, paper, lose).
+game(scissors, rock, lose).
+game(paper, scissors, lose).
+
+opponent_move("A", rock).
+opponent_move("B", paper).
+opponent_move("C", scissors).
+
+assume_move("X", rock).
+assume_move("Y", paper).
+assume_move("Z", scissors).
+
+assume_outcome("X", lose).
+assume_outcome("Y", draw).
+assume_outcome("Z", win).
+
+bonus(rock, 1).
+bonus(paper, 2).
+bonus(scissors, 3).
+bonus(lose, 0).
+bonus(draw, 3).
+bonus(win, 6).
+
+% Predicates
+
+get_data(Result) :-
+ setup_call_cleanup(open("2022/day-02/input", read, In),
+ (read_string(In, _, Str),
+ split_string(Str, "\n", "\s\t\n", Lines),
+ maplist([In, Out] >> split_string(In, "\s", "", Out), Lines, Result)),
+ close(In)).
+
+score_game(MyMove, Result, Score) :-
+ bonus(Result, X),
+ bonus(MyMove, Y),
+ Score is X + Y.
+
+part_1_score([Them, Me], Score) :-
+ opponent_move(Them, TheirMove),
+ assume_move(Me, MyMove),
+ game(MyMove, TheirMove, Result),
+ score_game(MyMove, Result, Score).
+
+part_1_total(Total) :-
+ get_data(Games),
+ maplist(part_1_score, Games, Scores),
+ sum_list(Scores, Total).
+
+part_2_score([Them, Outcome], Score) :-
+ opponent_move(Them, TheirMove),
+ assume_outcome(Outcome, Result),
+ game(MyMove, TheirMove, Result),
+ score_game(MyMove, Result, Score).
+
+part_2_total(Total) :-
+ get_data(Games),
+ maplist(part_2_score, Games, Scores),
+ sum_list(Scores, Total). \ No newline at end of file