aboutsummaryrefslogtreecommitdiff
path: root/aoc2022/day-03/day-03.ipynb
diff options
context:
space:
mode:
authorH.J <thechairman@thechairman.info>2024-10-09 11:36:55 -0400
committerH.J <thechairman@thechairman.info>2024-10-09 11:36:55 -0400
commit8777ff071f7bb37631baa7b6717ad29961e50911 (patch)
tree6d59c4ed58e454b960339c3d1151f0a879e8d7cb /aoc2022/day-03/day-03.ipynb
parent6156a9ef7be4012063a042aafb4e9b0d7eadde8e (diff)
downloadgleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.tar.gz
gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.zip
sorting by language
Diffstat (limited to 'aoc2022/day-03/day-03.ipynb')
-rw-r--r--aoc2022/day-03/day-03.ipynb168
1 files changed, 0 insertions, 168 deletions
diff --git a/aoc2022/day-03/day-03.ipynb b/aoc2022/day-03/day-03.ipynb
deleted file mode 100644
index 27b8086..0000000
--- a/aoc2022/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
-}