diff options
author | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
---|---|---|
committer | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
commit | 8777ff071f7bb37631baa7b6717ad29961e50911 (patch) | |
tree | 6d59c4ed58e454b960339c3d1151f0a879e8d7cb /racket/aoc2022/day-01 | |
parent | 6156a9ef7be4012063a042aafb4e9b0d7eadde8e (diff) | |
download | gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.tar.gz gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.zip |
sorting by language
Diffstat (limited to 'racket/aoc2022/day-01')
-rw-r--r-- | racket/aoc2022/day-01/day-01.ipynb | 139 | ||||
-rw-r--r-- | racket/aoc2022/day-01/day-01.rkt | 20 |
2 files changed, 159 insertions, 0 deletions
diff --git a/racket/aoc2022/day-01/day-01.ipynb b/racket/aoc2022/day-01/day-01.ipynb new file mode 100644 index 0000000..c79a3f6 --- /dev/null +++ b/racket/aoc2022/day-01/day-01.ipynb @@ -0,0 +1,139 @@ +{ + "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": [ + "<code>70374</code>" + ], + "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": [ + "<code>204610</code>" + ], + "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 +} diff --git a/racket/aoc2022/day-01/day-01.rkt b/racket/aoc2022/day-01/day-01.rkt new file mode 100644 index 0000000..5215014 --- /dev/null +++ b/racket/aoc2022/day-01/day-01.rkt @@ -0,0 +1,20 @@ +#lang racket + +(require advent-of-code + threading) + +(define calorie-data (fetch-aoc-input (find-session) 2022 1)) + +;; part 1 +(~>> calorie-data + (string-split _ "\n\n") + (map (λ~>> string-split (map string->number) (apply +))) + (apply max)) + +;; part 2 +(~>> calorie-data + (string-split _ "\n\n") + (map (λ~>> string-split (map string->number) (apply +))) + (sort _ >) + (take _ 3) + (apply +)) |