{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Advent of Code 2022\n", "#### Day 1: Calorie Counting\n", "\n", "Elves carry various amounts of food with various caloric contents.\n", "\n", "**Part 1.** How many calories is the elf with the most calories of food carrying?\n", "\n", "**Part 2.** How many calories are the three elves with the most calories of food carrying?" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "(require racket\n", " advent-of-code\n", " threading)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Part 1\n", "\n", "The data file is a list of integers, one on each line, with an empty line separating the inventory of each elf.\n", "\n", "1. Fetch the input file,\n", "2. split it on double newlines to find each elf's inventory,\n", "3. split each inventory on single newlines,\n", "4. convert each inventory member to a number,\n", "5. sum up each list, and\n", "6. find the maximum one. \n", "\n", "This is straightforward to do with threading/piping:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "70374" ], "text/plain": [ "70374" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(define calorie-data (fetch-aoc-input (find-session) 2022 1))\n", "\n", "(~>> calorie-data\n", " (string-split _ \"\\n\\n\")\n", " (map (λ~>> string-split (map string->number) (apply +)))\n", " (apply max))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Part 2\n", "\n", "Similarly, to find the top three calorie holders,\n", "\n", "1. fetch the input file,\n", "2. split it on double newlines,\n", "3. split each list member on single newlines,\n", "4. convert each sublist member to a number,\n", "5. sum up each list, \n", "6. sort the list from high to low,\n", "7. take the first three members,\n", "8. and sum them." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "204610" ], "text/plain": [ "204610" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(~>> calorie-data\n", " (string-split _ \"\\n\\n\")\n", " (map (λ~>> string-split (map string->number) (apply +)))\n", " (sort _ >)\n", " (take _ 3)\n", " (apply +))\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 }, "nbformat": 4, "nbformat_minor": 2 }