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-06/day-06.ipynb | |
parent | 6156a9ef7be4012063a042aafb4e9b0d7eadde8e (diff) | |
download | gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.tar.gz gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.zip |
sorting by language
Diffstat (limited to 'racket/aoc2022/day-06/day-06.ipynb')
-rw-r--r-- | racket/aoc2022/day-06/day-06.ipynb | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/racket/aoc2022/day-06/day-06.ipynb b/racket/aoc2022/day-06/day-06.ipynb new file mode 100644 index 0000000..0c89fa1 --- /dev/null +++ b/racket/aoc2022/day-06/day-06.ipynb @@ -0,0 +1,122 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Advent of Code 2022\n", + "#### Day 6: Tuning Trouble\n", + "\n", + "You're parsing a buffer of characters, looking for a \"marker\": the index of the first character where the previous $n$ characters are all unique.\n", + "\n", + "**Part 1.** What if $n = 4$, for the \"start of packet\" marker?\n", + "\n", + "**Part 2.** What if $n = 14$, for the \"start of message\" marker?" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "(require racket\n", + " advent-of-code\n", + " (only-in algorithms sliding))\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### Parts 1 and 2\n", + "\n", + "`(sliding xs n)` returns all the overlapping sublists of `xs` that are `n` elements wide. This turns this problem into a simple `for` comprehension, just looking for the first sublist with no duplicates (which means its length after deduplication doesn't change).\n", + "\n", + "The solution to part 1 and part 2 is the same, just differing in how wide the window is." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "<code>1042</code>" + ], + "text/plain": [ + "1042" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(define buffer (fetch-aoc-input (find-session) 2022 6))\n", + "\n", + "(define (find-marker data type)\n", + " (define n\n", + " (match type\n", + " ['start-of-packet 4]\n", + " ['start-of-message 14]))\n", + " (for/first ([chunk (in-list (sliding (string->list data) n))]\n", + " [i (in-naturals n)]\n", + " #:when (= n (~> chunk remove-duplicates length)))\n", + " i))\n", + "\n", + "(find-marker buffer 'start-of-packet)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "<code>2980</code>" + ], + "text/plain": [ + "2980" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(find-marker buffer 'start-of-message)" + ] + } + ], + "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": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} |