aboutsummaryrefslogtreecommitdiff
path: root/2022/day-03/day-03.ipynb
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-12-03 01:18:45 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-12-03 01:18:45 -0500
commit4ce0a72964a42057b4c6f2bfaca5cfea56bd0eb4 (patch)
tree32207f9665bbfcec0794ff9c26363f3fd0a3123f /2022/day-03/day-03.ipynb
parentf5c99dbecabad67504aa9b914d1ab002208862fd (diff)
downloadgleam_aoc-4ce0a72964a42057b4c6f2bfaca5cfea56bd0eb4.tar.gz
gleam_aoc-4ce0a72964a42057b4c6f2bfaca5cfea56bd0eb4.zip
day 3 complete
Diffstat (limited to '2022/day-03/day-03.ipynb')
-rw-r--r--2022/day-03/day-03.ipynb155
1 files changed, 150 insertions, 5 deletions
diff --git a/2022/day-03/day-03.ipynb b/2022/day-03/day-03.ipynb
index 3a07f95..4d5e5b4 100644
--- a/2022/day-03/day-03.ipynb
+++ b/2022/day-03/day-03.ipynb
@@ -3,21 +3,166 @@
{
"cell_type": "markdown",
"metadata": {},
- "source": []
+ "source": [
+ "### Advent of Code 2022\n",
+ "#### Day 3: Rucksack Reorganization\n",
+ "\n",
+ "**Part 1.** Every elf has a rucksack with two compartments. What's the total priority value of the items that appear in both compartments of each sack?\n",
+ "\n",
+ "**Part 2.** Each group of three elves is carrying exactly one item in common. What's the total priority value of those common items?\n",
+ "\n",
+ "To ease the conversion between strings, lists and sets I bring in some utility functions from `relation`, and I also bring in one utility function for part 2 from `algorithms`. "
+ ]
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 1,
+ "metadata": {
+ "vscode": {
+ "languageId": "racket"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "(require racket\n",
+ " advent-of-code\n",
+ " threading\n",
+ " (only-in relation ->list ->set ->char)\n",
+ " (only-in algorithms chunks-of))\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
"metadata": {},
+ "source": [
+ "The items in each elf's inventory are represented by a string of alphabetic (case-sensitive) characters, each with a \"priority value\":\n",
+ "* Lowercase item types `a` through `z` have priorities 1 through 26.\n",
+ "* Uppercase item types `A` through `Z` have priorities 27 through 52."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "vscode": {
+ "languageId": "racket"
+ }
+ },
"outputs": [],
- "source": []
+ "source": [
+ "(define priority\n",
+ " (for/hash ([i (in-naturals 1)]\n",
+ " [c (in-sequences (inclusive-range 97 122) (inclusive-range 65 90))])\n",
+ " (values (->char c) i)))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##### Part 1\n",
+ "\n",
+ "The front half and back half of the rucksack have one item in common we need to identify. For each rucksack in the inventory, we apply the following steps:\n",
+ "\n",
+ "1. Split the bag into the two halves,\n",
+ "2. find the common item in both halves (the intersection of the sets),\n",
+ "3. then look up that common item's value. "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "vscode": {
+ "languageId": "racket"
+ }
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "<code>7845</code>"
+ ],
+ "text/plain": [
+ "7845"
+ ]
+ },
+ "execution_count": 3,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "(define raw-inventory (~> (fetch-aoc-input (find-session) 2022 3) string-split))\n",
+ "\n",
+ "(for/sum ([bag (in-list raw-inventory)])\n",
+ " (define len (/ (string-length bag) 2))\n",
+ " (~>> bag\n",
+ " ->list\n",
+ " ((λ (xs) (list (take xs len) (drop xs len)))) ; step 1\n",
+ " (apply set-intersect _) ; step 2\n",
+ " set-first\n",
+ " (hash-ref priority))) ; step 3\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##### Part 2\n",
+ "\n",
+ "Now we need to take three rucksacks at a time and find the common item they all share. The procedure is simpler than Part 1's, especially with the `chunks-of` function that automatically forms groups of three."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {
+ "vscode": {
+ "languageId": "racket"
+ }
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "<code>2790</code>"
+ ],
+ "text/plain": [
+ "2790"
+ ]
+ },
+ "execution_count": 4,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "(for/sum ([bags (in-list (chunks-of raw-inventory 3))])\n",
+ " (~>> bags (map ->set) (apply set-intersect) set-first (hash-ref priority)))\n"
+ ]
}
],
"metadata": {
+ "kernelspec": {
+ "display_name": "Racket (Trusted)",
+ "language": "racket",
+ "name": "racket-trusted"
+ },
"language_info": {
- "name": "python"
+ "codemirror_mode": "scheme",
+ "file_extension": ".rkt",
+ "mimetype": "text/x-racket",
+ "name": "Racket",
+ "pygments_lexer": "racket",
+ "version": "8.7"
},
- "orig_nbformat": 4
+ "orig_nbformat": 4,
+ "vscode": {
+ "interpreter": {
+ "hash": "7454d72389401259f8ab87ac90deac92d19baedf4a52e60301852b1f4c653c5c"
+ }
+ }
},
"nbformat": 4,
"nbformat_minor": 2