diff options
author | Hunky Jimpjorps <thechairman@thechairman.info> | 2022-11-29 13:09:46 -0500 |
---|---|---|
committer | Hunky Jimpjorps <thechairman@thechairman.info> | 2022-11-29 13:09:46 -0500 |
commit | f803bbca50f6b9faa4107a57b86c66383d253ec2 (patch) | |
tree | 2bca81ff6aa2ad5c79f3f96b40dd1d42cbf52560 /2020/day-08/day-08.ipynb | |
parent | c871d9cbe4cc6d3063665a428de4b205cef29041 (diff) | |
download | gleam_aoc-f803bbca50f6b9faa4107a57b86c66383d253ec2.tar.gz gleam_aoc-f803bbca50f6b9faa4107a57b86c66383d253ec2.zip |
day 7 tweaking, part 1 of day 8
Diffstat (limited to '2020/day-08/day-08.ipynb')
-rw-r--r-- | 2020/day-08/day-08.ipynb | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/2020/day-08/day-08.ipynb b/2020/day-08/day-08.ipynb new file mode 100644 index 0000000..39d4b8e --- /dev/null +++ b/2020/day-08/day-08.ipynb @@ -0,0 +1,114 @@ +{ + "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": [ + "<code>'(acc . 49)</code>" + ], + "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": [ + "<code>1949</code>" + ], + "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 +} |