From 8777ff071f7bb37631baa7b6717ad29961e50911 Mon Sep 17 00:00:00 2001 From: "H.J" Date: Wed, 9 Oct 2024 11:36:55 -0400 Subject: sorting by language --- racket/aoc2022/day-06/day-06.ipynb | 122 +++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 racket/aoc2022/day-06/day-06.ipynb (limited to 'racket/aoc2022/day-06/day-06.ipynb') 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": [ + "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 +} -- cgit v1.2.3