{ "cells": [ { "cell_type": "code", "execution_count": 17, "metadata": { "vscode": { "languageId": "racket" } }, "outputs": [], "source": [ "#lang iracket/lang #:require racket\n", "(require advent-of-code\n", " threading\n", " rebellion/collection/entry\n", " rebellion/collection/multidict)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "vscode": { "languageId": "racket" } }, "outputs": [ { "data": { "text/html": [ "'(acc . 49)" ], "text/plain": [ "'(acc . 49)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(define raw-instructions (~> (fetch-aoc-input (find-session) 2020 8) (string-split \"\\n\")))\n", "\n", "(define instruction-set (for/hash ([instruction (in-list raw-instructions)] [i (in-naturals)])\n", " (match-define (list op val) (string-split instruction))\n", " (values i (cons (string->symbol op) (string->number val)))))\n", "\n", "(hash-ref instruction-set 0)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "vscode": { "languageId": "racket" } }, "outputs": [ { "data": { "text/html": [ "1949" ], "text/plain": [ "1949" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(define (execute [instruction (hash-ref instruction-set 0)] [acc 0] [line 0] [visited (set)])\n", " (match instruction\n", " [_\n", " #:when (set-member? visited line)\n", " acc]\n", " [(cons 'acc n)\n", " (define new-line (add1 line))\n", " (execute (hash-ref instruction-set new-line) (+ acc n) new-line (set-add visited line))]\n", " [(cons 'jmp n)\n", " (define new-line (+ n line))\n", " (execute (hash-ref instruction-set new-line) acc new-line (set-add visited line))]\n", " [(cons 'nop _)\n", " (define new-line (add1 line))\n", " (execute (hash-ref instruction-set new-line) acc new-line (set-add visited line))]))\n", "\n", "(execute)\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 }