diff options
author | J.J <thechairman@thechairman.info> | 2023-11-30 17:10:00 -0500 |
---|---|---|
committer | J.J <thechairman@thechairman.info> | 2023-11-30 17:10:00 -0500 |
commit | 8ab65dc2da1742eb86ec636c50c7018385b68167 (patch) | |
tree | c4fd556aca9b867cfa1f2f174128c30857353884 /2022/day-03 | |
parent | fafbeaf9e3c09ba7a5bea7e47d5736001f8a5aa1 (diff) | |
download | gleam_aoc-8ab65dc2da1742eb86ec636c50c7018385b68167.tar.gz gleam_aoc-8ab65dc2da1742eb86ec636c50c7018385b68167.zip |
prep for 2023, renaming for consistency
Diffstat (limited to '2022/day-03')
-rw-r--r-- | 2022/day-03/day-03.ipynb | 168 |
1 files changed, 0 insertions, 168 deletions
diff --git a/2022/day-03/day-03.ipynb b/2022/day-03/day-03.ipynb deleted file mode 100644 index 27b8086..0000000 --- a/2022/day-03/day-03.ipynb +++ /dev/null @@ -1,168 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "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`. " - ] - }, - { - "cell_type": "code", - "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" - ] - }, - { - "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": [ - "(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 `in-slice` 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-slice 3 raw-inventory)])\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": { - "codemirror_mode": "scheme", - "file_extension": ".rkt", - "mimetype": "text/x-racket", - "name": "Racket", - "pygments_lexer": "racket", - "version": "8.7" - }, - "orig_nbformat": 4, - "vscode": { - "interpreter": { - "hash": "7454d72389401259f8ab87ac90deac92d19baedf4a52e60301852b1f4c653c5c" - } - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} |