{ "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": [ "1042" ], "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": [ "2980" ], "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 }